Solved

MS Access VBA to copy workshets in order

Posted on 2013-05-30
6
331 Views
Last Modified: 2013-05-31
Hello Experts,

The below code copies specific worksheets into a Master spreadsheet but does not copy them in order.  It would be great to use the ID field and you can see at one time, I tried using the “SSTabOrder” field.  Is there a way to modify the below code as I tried using Before and After in the statement, “shtToCopy.Copy After:=wkbSource.Sheets(wkbSource.Sheets.Count)”?


ID      SSName      SSTab      SSTabOut      SSPDFOut      SSTabOrder
1      S:\2013 reports\cfi 0413.xlsm      Output 1      Output 1      CASCI      1cfi 0313.xlsm
2      S:\2013 reports\cfmi 0413.xlsm      Output 1      Output 1 (2)      NCIA      2cfmi 0313.xlsm
3      S:\CFMI Close\CFMI RiskNEW.xlsm      Output 1 (100.T05)      Output 1 (100.T05)      NCAS      3aCFMI RiskNEW8.xlsm
4      S:\CFMI Close\CFMI RiskNEW.xlsm      Output 1 (100.U65.050)      Output 1 (100.U65.050)      Willse      3bCFMI RiskNEW8.xlsm
5      S:\CFMI Close\CFMI RiskNEW.xlsm      Output 1 (100.O65.050)      Output 1 (100.O65.050)      Care      3cCFMI RiskNEW8.xlsm

VBA:

Private Sub TestCopy_Click()
Dim xl As New Excel.Application
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shtToCopy As Excel.Worksheet
Dim rs As Recordset
Dim SSName As String
Dim SSTab As String
Dim shtName As String
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set rs = CurrentDb.OpenRecordset("TblReports")
rs.MoveFirst
Set wkbDest = xl.Workbooks.Open("H:\PDF\MasterReport.xlsx")
Do While Not (rs.EOF)
    shtName = rs!SSTab
    Set wkbSource = xl.Workbooks.Open(rs("SSName"))
    Set shtToCopy = wkbSource.Sheets(shtName)
    shtToCopy.Copy After:=wkbSource.Sheets(wkbSource.Sheets.Count)
        Set shtToCopy = wkbSource.Sheets(shtName)           'Makes a copy of "Sheet1"
    shtToCopy.Copy wkbDest.Sheets(1)
    Set shtToCopy = wkbSource.Sheets(shtName)
    wkbSource.Close False
  rs.MoveNext
Loop
wkbDest.Close SaveChanges:=True
Set wkbSource = Nothing
Set wkbDest = Nothing
    Beep
    MsgBox "Worksheet was copied"

End Sub
0
Comment
Question by:CFMI
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39208810
I don't quite understand the SSTabOrder column - it does not appear to contain a number.  If it did you could use it to insert the sheets correctly, but it would help to use a sql query to return the records in ssTabOrder sequence.  Can you clarify, please?
0
 
LVL 1

Author Comment

by:CFMI
ID: 39208848
Hello,

I am under the impression that the code opens, "TblReports" (an Access Table with the Workbooks and worksheets in oder, via ID) and the statement:
 "shtToCopy.Copy After:=wkbSource.Sheets(wkbSource.Sheets.Count)"
puts the worksheets next to the previous worksheet but for some reason the worksheets are out of order.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39209256
I think there' some confusion in your code. I can't test it properly without setting up a lot of stuff, but try this:
Do While Not (rs.EOF)
    shtName = rs!SSTab
    Set wkbSource = xl.Workbooks.Open(rs("SSName"))
        Set shtToCopy = wkbSource.Sheets(shtName)         
    shtToCopy.Copy After:=wkbDest.Sheets(wbkDest.Sheets.Count)
    wkbSource.Close xlDoNotSaveChanges
  rs.MoveNext
Loop

Open in new window

The original code made a copy of the source sheet after the last sheet in the source workbook, then copied the source sheet as the first sheet in destination.  Were the sheets in reverse order?  I have removed the redundant code and changed it so the new sheet is always the last in the new workbook.   This does not use any information from the table to order the sheets, except the order they arrive in.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 1

Author Comment

by:CFMI
ID: 39210365
Good Morning,

Using the above code produces an error message, "Objest Required" and debugging points to "shtToCopy.Copy After:=wkbDest.Sheets(wbkDest.Sheets.Count)".

Thanks
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 39210534
Sorry - silly typo - should be wkbDest, not wbkDest.  I always use 'wbk' as the prefix for Excel workbooks and I misread your code as that.
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 39211344
Excellent, your provided code worked well - Thank you!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question