Solved

MS Access VBA to copy workshets in order

Posted on 2013-05-30
6
329 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now