Solved

MS Access VBA to copy workshets in order

Posted on 2013-05-30
6
330 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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now