acramer_dominium
asked on
Adding Tab, Copying Range to Excel via Access VBA
Experts. I have a program in Access that I'm using to dump a query to excel. Once it's in excel I want to add a second worksheet, name the worksheets, copy all data from the first worksheet to the second and sort the second sheet by a different column.
Below is the code I have so far. When I run the code, the spreadsheet opens and the name of the first tab changes and then it ignores the rest of the code - no error. If I close the spreadsheet while still in access and run the code again, it works perfectly. Any ideas?!
>>>>>>
Dim stDocName As String
stDocName = "qryExceptionsbyProgram"
DoCmd.OutputTo acOutputQuery, stDocName, acFormatXLS, " " & stDocName & ".xls", True
'copy the contents of the first tab to a second added tab
Dim ExcelObject As Excel.Application
On Error Resume Next
' if excel is already open it uses that instance
Set ExcelObject = GetObject(, "Excel.Application")
' if excel isn't open, it opens a new instance of excel
If ExcelObject Is Nothing Then
Set ExcelObject = New Excel.Application
ExcelObject.EnableEvents = False
End If
ExcelObject.Application.Sh eets("qryE xceptionsb yProgram") .Select
ExcelObject.Application.Ac tiveSheet. Name = "Sorted by Property Name"
ExcelObject.Application.Sh eets.Add After:=ActiveSheet
ExcelObject.Application.Ac tiveSheet. Name = "Sorted by Program Type"
ExcelObject.Application.Sh eets("Sort ed by Property Name").Range("a:e").Copy
ExcelObject.Application.Ra nge("a1"). Select
ExcelObject.Application.Ac tiveSheet. Paste
'Apply sorting to the second tab
ExcelObject.Application.Co lumns("A:E ").Sort key1:=Range("C2"), _
order1:=xlAscending, Header:=xlYes
ExcelObject.Application.Ac tiveSheet. Name = "Sorted by Property Name"
>>>>>>>
Below is the code I have so far. When I run the code, the spreadsheet opens and the name of the first tab changes and then it ignores the rest of the code - no error. If I close the spreadsheet while still in access and run the code again, it works perfectly. Any ideas?!
>>>>>>
Dim stDocName As String
stDocName = "qryExceptionsbyProgram"
DoCmd.OutputTo acOutputQuery, stDocName, acFormatXLS, " " & stDocName & ".xls", True
'copy the contents of the first tab to a second added tab
Dim ExcelObject As Excel.Application
On Error Resume Next
' if excel is already open it uses that instance
Set ExcelObject = GetObject(, "Excel.Application")
' if excel isn't open, it opens a new instance of excel
If ExcelObject Is Nothing Then
Set ExcelObject = New Excel.Application
ExcelObject.EnableEvents = False
End If
ExcelObject.Application.Sh
ExcelObject.Application.Ac
ExcelObject.Application.Sh
ExcelObject.Application.Ac
ExcelObject.Application.Sh
ExcelObject.Application.Ra
ExcelObject.Application.Ac
'Apply sorting to the second tab
ExcelObject.Application.Co
order1:=xlAscending, Header:=xlYes
ExcelObject.Application.Ac
>>>>>>>
ASKER
Tried that and it behaved the same way. I've attached an example database with all my objects in it. There is one form with two different buttons. The buttons are doing the same routine just with different data.
When you click a button, spreadsheet pops up not complete. Close spreadsheet. Click button again, spreadsheet pops up complete.
ExcelExample.mdb
When you click a button, spreadsheet pops up not complete. Close spreadsheet. Click button again, spreadsheet pops up complete.
ExcelExample.mdb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Get rid of the DoEvents and add in a "Save"
This worked for me...
ExcelObject.Application.Sh eets("qryE xceptionsb yProgram") .Select
ExcelObject.Application.Ac tiveSheet. Name = "Sorted by Property Name"
ExcelObject.Application.Sh eets.Add After:=ActiveSheet
ExcelObject.Application.Ac tiveSheet. Name = "Sorted by Program Type"
ExcelObject.ActiveWorkbook .Save
ExcelObject.Application.Sh eets("Sort ed by Property Name").Range("a:f").Copy
ExcelObject.Application.Ra nge("a1"). Select
ExcelObject.Application.Ac tiveSheet. Paste
JeffCoachman
This worked for me...
ExcelObject.Application.Sh
ExcelObject.Application.Ac
ExcelObject.Application.Sh
ExcelObject.Application.Ac
ExcelObject.ActiveWorkbook
ExcelObject.Application.Sh
ExcelObject.Application.Ra
ExcelObject.Application.Ac
JeffCoachman
just for fun, try mu suggestion as well
ASKER
Works every time
Add a line:
DoEvents
....after the line that names the sheet...