Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access VBA to copy workshets in order

Posted on 2013-05-30
6
Medium Priority
?
335 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 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