CFMI
asked on
MS Access VBA to copy Workbook Worksheet to another Workbook
Hello Experts,
Please provide assistance to include a MS Access table field that displays the Path and the File Name along with the Tab name as I am receiving a Run Time Error 1004 that displays " Output 1.xlsx could not be found". I first tried the Spreadsheet Name then entered the Spreadsheet Tab inside the boded below statement – this is where the code stops. The Access table looks like:
ID SpreadsheetName SpreadsheetTab
1 X:\2013 reports\cfi consol nod 0313.xlsm Output 1
2 X:\2013 reports\cfmi consol 0313.xlsm Output 1
3 X:\Reports Close\CFMI Pre_Close_w_filters_RiskNE W.xlsm Output 1 (100.100.T05)
and the VBA:
Private Sub WorksheetsCopy_Click()
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
Dim SpreadsheetName As String
Dim SpreadsheetTab As String
Dim ID As Field
Dim rs As Recordset
Dim sh As Worksheet
Dim oldPath As String, newPath As String
''Late binding
Set xl = CreateObject("Excel.Applic ation")
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T blReports" )
Do While Not (rs.EOF)
Dim wkbSource As Excel.workbook
Dim wkbDest As Excel.workbook
Set wkbSource = Workbooks.Open(xl.Workbook s.Open(rs( "Spreadshe etTab")))
Set wkbDest = Workbooks.Open("H:\PDF\Mas terReport. xlsx")
'Opens workbook
Workbooks.Open "wkbSource"
'Makes a copy of "Sheet1"
ActiveWorkbook.Sheets("She et1").Copy _
after:=ActiveWorkbook.Shee ts("Sheet1 ")
'Copies that copy to "wkbDest" workbook
ActiveSheet.Move Before:=Workbooks("wkbDest ").Sheets( 1)
ActiveSheet.Next.Select
'Closes "wkbDest" workbook and saves the copied sheeet
ActiveWorkbook.Close SaveChanges:=True
rs.MoveNext
CopyFrom.Close False
Loop
rs.Close
End Sub
Thanks,
Bob
Please provide assistance to include a MS Access table field that displays the Path and the File Name along with the Tab name as I am receiving a Run Time Error 1004 that displays " Output 1.xlsx could not be found". I first tried the Spreadsheet Name then entered the Spreadsheet Tab inside the boded below statement – this is where the code stops. The Access table looks like:
ID SpreadsheetName SpreadsheetTab
1 X:\2013 reports\cfi consol nod 0313.xlsm Output 1
2 X:\2013 reports\cfmi consol 0313.xlsm Output 1
3 X:\Reports Close\CFMI Pre_Close_w_filters_RiskNE
and the VBA:
Private Sub WorksheetsCopy_Click()
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
Dim SpreadsheetName As String
Dim SpreadsheetTab As String
Dim ID As Field
Dim rs As Recordset
Dim sh As Worksheet
Dim oldPath As String, newPath As String
''Late binding
Set xl = CreateObject("Excel.Applic
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T
Do While Not (rs.EOF)
Dim wkbSource As Excel.workbook
Dim wkbDest As Excel.workbook
Set wkbSource = Workbooks.Open(xl.Workbook
Set wkbDest = Workbooks.Open("H:\PDF\Mas
'Opens workbook
Workbooks.Open "wkbSource"
'Makes a copy of "Sheet1"
ActiveWorkbook.Sheets("She
after:=ActiveWorkbook.Shee
'Copies that copy to "wkbDest" workbook
ActiveSheet.Move Before:=Workbooks("wkbDest
ActiveSheet.Next.Select
'Closes "wkbDest" workbook and saves the copied sheeet
ActiveWorkbook.Close SaveChanges:=True
rs.MoveNext
CopyFrom.Close False
Loop
rs.Close
End Sub
Thanks,
Bob
ASKER
Using the above code received the same error message, "Run Time Error 1004 as Output 1.xlsx could not be found" - this points to Line #19; please help.
Private Sub WorksheetsCopy_Click()
Dim xl As Object
Dim rs As DAO.Recordset
Dim wkbSource As Excel.workbook
Dim wkbDest As Excel.workbook
''Late binding
Set xl = CreateObject("Excel.Applic ation")
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T blReports" )
Set wkbDest = xl.Workbooks.Open("H:\PDF\ MasterRepo rt.xlsx")
Do While Not (rs.EOF)
Set wkbSource = xl.Workbooks.Open(rs("Spre adsheetNam e"))
'Makes a copy of "Sheet1"
wkbSource.Worksheets(rs("S preadsheet Name")).Co py _
Before:=wkbDest.Sheets(1)
wkbSource.Close False
rs.MoveNext
Loop
'Closes "wkbDest" workbook and saves the copied sheeet
wkbDest.Close SaveChanges:=True
rs.Close
Set wkbSource = Nothing
Set wkbDest = Nothing
xl.Quit
Set xl = Nothing
End Sub
Dim xl As Object
Dim rs As DAO.Recordset
Dim wkbSource As Excel.workbook
Dim wkbDest As Excel.workbook
''Late binding
Set xl = CreateObject("Excel.Applic
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T
Set wkbDest = xl.Workbooks.Open("H:\PDF\
Do While Not (rs.EOF)
Set wkbSource = xl.Workbooks.Open(rs("Spre
'Makes a copy of "Sheet1"
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets(1)
wkbSource.Close False
rs.MoveNext
Loop
'Closes "wkbDest" workbook and saves the copied sheeet
wkbDest.Close SaveChanges:=True
rs.Close
Set wkbSource = Nothing
Set wkbDest = Nothing
xl.Quit
Set xl = Nothing
End Sub
ASKER
The above code receives a "Type Mismatched" error message and points to:
wkbSource.Worksheets(rs("S preadsheet Name")).Co py _
Before:=wkbDest.Sheets(1)
My guess is the Worsheet name is "Output 1" and it expects "Sheet 1". The destination is to a Workbook with a worksheet named, "Sheet 1".
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets(1)
My guess is the Worsheet name is "Output 1" and it expects "Sheet 1". The destination is to a Workbook with a worksheet named, "Sheet 1".
Sorry, that should be:
wkbSource.Worksheets(rs("S preadsheet Tab")).Cop y _
Before:=wkbDest.Sheets(1)
Now, that places the copied worksheet before the first sheet overall. To place it before "Sheet 1"...
wkbSource.Worksheets(rs("S preadsheet Tab")).Cop y _
Before:=wkbDest.Sheets("Sh eet 1")
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets(1)
Now, that places the copied worksheet before the first sheet overall. To place it before "Sheet 1"...
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets("Sh
ASKER
We now have anther error message stating, "Subscript out of range" using:
wkbSource.Worksheets(rs("S preadsheet Tab")).Cop y _
Before:=wkbDest.Sheets("Sh eet 1")
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets("Sh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Both workbooks open, then I see the error message and the workbooks remain opened and are the correct workbooks but nothing is copied. The error message states, "Subscript out of range" and debugging points to:
wkbSource.Worksheets(rs("S preadsheet Tab")).Cop y _
Before:=wkbDest.Sheets("Sh eet 1")")
Then I changed it to: Before:=wkbDest.Sheets("Sh eet1")") and I received Type mismatch.
Overall, I am reading in 53 specific Workbook, worksheets and coping them to a Master Workbook with 53 worksheets; perhaps I do not already need a spreadsheet with 53 worksheets especially since it doesn’t even seem to be copying?
Current code:
Private Sub TestCopy_Click()
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
Dim SpreadsheetName As String
Dim SpreadsheetTab As String
Dim ID As Field
Dim rs As Recordset
Dim sh As Worksheet
Dim oldPath As String, newPath As String
Dim wkbSource As Excel.Workbook
Dim wkbDest As Excel.Workbook
''Late binding
Set xl = CreateObject("Excel.Applic ation")
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T blReports" )
'Set wkbSource = xl.Workbooks.Open(xl.Workb ooks.Open( rs("Spread sheetTab") ))
Set wkbSource = xl.Workbooks.Open(rs("Spre adsheetNam e"))
Set wkbDest = xl.Workbooks.Open("H:\PDF\ MasterRepo rt.xlsx")
Do While Not (rs.EOF)
Set wkbSource = xl.Workbooks.Open(rs("Spre adsheetNam e"))
Set wkbDest = xl.Workbooks.Open("H:\PDF\ MasterRepo rt.xlsx")
'Makes a copy of "Sheet1"
wkbSource.Worksheets(rs("S preadsheet Tab")).Cop y _
Before:=wkbDest.Sheets("Sh eet 1")
'Closes "wkbDest" workbook and saves the copied sheeet
wkbSource.Close False
rs.MoveNext
Loop
'Closes "wkbDest" workbook and saves the copied sheet
wkbDest.Close SaveChanges:=True
wkbSource.Close False
rs.Close
Set wkbSource = Nothing
Set wkbDest = Nothing
xl.Quit
Set xl = Nothing
End Sub
MasterReport.xlsx
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets("Sh
Then I changed it to: Before:=wkbDest.Sheets("Sh
Overall, I am reading in 53 specific Workbook, worksheets and coping them to a Master Workbook with 53 worksheets; perhaps I do not already need a spreadsheet with 53 worksheets especially since it doesn’t even seem to be copying?
Current code:
Private Sub TestCopy_Click()
Dim CopyFrom As Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object
Dim SpreadsheetName As String
Dim SpreadsheetTab As String
Dim ID As Field
Dim rs As Recordset
Dim sh As Worksheet
Dim oldPath As String, newPath As String
Dim wkbSource As Excel.Workbook
Dim wkbDest As Excel.Workbook
''Late binding
Set xl = CreateObject("Excel.Applic
xl.Visible = True
newPath = "H:\PDF"
Set rs = CurrentDb.OpenRecordset("T
'Set wkbSource = xl.Workbooks.Open(xl.Workb
Set wkbSource = xl.Workbooks.Open(rs("Spre
Set wkbDest = xl.Workbooks.Open("H:\PDF\
Do While Not (rs.EOF)
Set wkbSource = xl.Workbooks.Open(rs("Spre
Set wkbDest = xl.Workbooks.Open("H:\PDF\
'Makes a copy of "Sheet1"
wkbSource.Worksheets(rs("S
Before:=wkbDest.Sheets("Sh
'Closes "wkbDest" workbook and saves the copied sheeet
wkbSource.Close False
rs.MoveNext
Loop
'Closes "wkbDest" workbook and saves the copied sheet
wkbDest.Close SaveChanges:=True
wkbSource.Close False
rs.Close
Set wkbSource = Nothing
Set wkbDest = Nothing
xl.Quit
Set xl = Nothing
End Sub
MasterReport.xlsx
ASKER
The code worked well but I was provided an incorrect worksheet title and this creates an out of sequence error so my learning, verify all input FIRST - Thanks!
Open in new window