Solved

MS Access VBA to copy workshets in order

Posted on 2013-05-30
6
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

617 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