-
1
- #1
JSchumacher
Technical User
I spent quite a bit of time yesterday finding a solution to a problem, and I did get it to work, but I feel sure there is a much easier way to accomplish it. I welcome any ideas on making this work better.
I have a Union query that returns 48 rows (theoretically this number is consistent, realistically...?). I want to place those rows in an existing Excel spreadsheet. To do this I outputted the results to a new spreadsheet and then copied and pasted to the existing spreadsheet.
Here is the code I came up with:
Thanks for any ideas!
Judie
I have a Union query that returns 48 rows (theoretically this number is consistent, realistically...?). I want to place those rows in an existing Excel spreadsheet. To do this I outputted the results to a new spreadsheet and then copied and pasted to the existing spreadsheet.
Here is the code I came up with:
Code:
Private Sub RunVolumeReport()
Dim objXL As Excel.Application
Dim xlWBMain As Excel.Workbook
Dim xlWSMain As Excel.Worksheet
Dim xlWBNew As Excel.Workbook
Dim xlWSNew As Excel.Worksheet
Set objXL = CreateObject("Excel.Application")
Set xlWBMain = objXL.Workbooks.Open("s:\import files\volume.xls")
Set xlWSMain = xlWBMain.Worksheets(2)
objXL.Visible = True
xlWSMain.Activate
DoCmd.OutputTo acOutputQuery, "qryVolume", acFormatXLS, "s:\import files\NewVolume.xls"
Set xlWBNew = objXL.Workbooks.Open("s:\import files\NewVolume.xls")
Set xlWSNew = xlWBNew.Worksheets(1)
xlWSNew.Range("a2:d49").Copy
xlWSMain.Range("a2:d49").PasteSpecial
xlWSNew.Range("a2:d49").ClearOutline
'Close Excel
xlWBMain.Close True 'save changes to Main page
xlWBNew.Close False
objXL.Quit
'Release objects from memory
Set xlWSMain = Nothing
Set xlWBMain = Nothing
Set xlWSNew = Nothing
Set xlWBNew = Nothing
Set objXL = Nothing
End Sub
Thanks for any ideas!
Judie