Link to home
Start Free TrialLog in
Avatar of acramer_dominium
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.Sheets("qryExceptionsbyProgram").Select
ExcelObject.Application.ActiveSheet.Name = "Sorted by Property Name"
ExcelObject.Application.Sheets.Add After:=ActiveSheet
ExcelObject.Application.ActiveSheet.Name = "Sorted by Program Type"
ExcelObject.Application.Sheets("Sorted by Property Name").Range("a:e").Copy
ExcelObject.Application.Range("a1").Select
ExcelObject.Application.ActiveSheet.Paste

'Apply sorting to the second tab
ExcelObject.Application.Columns("A:E").Sort key1:=Range("C2"), _
      order1:=xlAscending, Header:=xlYes

ExcelObject.Application.ActiveSheet.Name = "Sorted by Property Name"

>>>>>>>
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

very wild guess..
Add a line:

    DoEvents
....after the line that names the sheet...
Avatar of acramer_dominium
acramer_dominium

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
ASKER CERTIFIED SOLUTION
Avatar of acramer_dominium
acramer_dominium

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Get rid of the DoEvents and add in a "Save"
This worked for me...


ExcelObject.Application.Sheets("qryExceptionsbyProgram").Select
ExcelObject.Application.ActiveSheet.Name = "Sorted by Property Name"
ExcelObject.Application.Sheets.Add After:=ActiveSheet
ExcelObject.Application.ActiveSheet.Name = "Sorted by Program Type"

ExcelObject.ActiveWorkbook.Save

ExcelObject.Application.Sheets("Sorted by Property Name").Range("a:f").Copy
ExcelObject.Application.Range("a1").Select
ExcelObject.Application.ActiveSheet.Paste


JeffCoachman
just for fun, try mu suggestion as well
Works every time