Exporting multiple queries to one excel file using vba

Hello E Experts:

I have found a few posts on this subject but they don't quite cover all the information I need. I have an Access query that has 12 months of data (there is a month field where the user inputs which month to query). I would like to cycle through this query and export each months' data into a  new separate worksheet in one existing excel file.

The primary problem is getting the VBA to open file, and populate the worksheets. Using the following code, I get a "User-Defined Type Not Defined" error. I read a lot on 'late binding' vs 'early binding', etc. Its been a while since I coded in VBA so I cannot remember whether those would have any significance with this issue.

Dim oApp as Excel.Application
Dim oFile as Excel.Workbook
Dim oSheet as Excel.Worksheet

I've not used the "Excel." parameter before in access 2007.

So, I be much appreciative of some guidance that provides a solution to addressing the above error and how to automate cycling through of the various months for export.

Thanks in advance!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
Did you set a VBA reference to Excel?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
Open the VBA editor alt-F11

 VBA Tool References
then select Excel

 Excel Object
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
The code you posted using Early Binding so you have to set the reference
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

It's quite straight forward to do what you're after, but rather than give you code that isn't quite right can you post a sample of the sort of data returned by your query?
This should get you started with exporting the worksheets:
Option Explicit
Option Compare Database

Public Sub ExportData()

    ' Declare constants
    Const SPREADSHEET_NAME As String = "C:\Users\Jez\Documents\Business\ExpertsExchange\Q2692801.xlsx"  ' Spreadsheet name

    ' Declare variables
    Dim appExcel As Excel.Application    ' Excel application
    Dim bytMonth As Byte                 ' Month
    Dim wkbWorkbook As Excel.Workbook    ' Excel workbook
    Dim wksWorksheet As Excel.Worksheet  ' Excel worksheet
    Dim rstRecordset As DAO.Recordset    ' Record set
    Dim strSQL As String                 ' SQL

    ' Open spreadsheet
    Set appExcel = New Excel.Application
    Set wkbWorkbook = appExcel.Workbooks.Open(SPREADSHEET_NAME)

    For bytMonth = 1 To 12

        ' Open record set
        strSQL = "SELECT * " & _
                 "FROM YourTable " & _
                 "WHERE Month = " & bytMonth
        Set rstRecordset = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)

        ' Copy data to new worksheet
        Set wksWorksheet = wkbWorkbook.Worksheets.Add(, appExcel.ActiveWorkbook.ActiveSheet)  ' After 1st worksheet
        wksWorksheet.Name = MonthName(bytMonth)
        wksWorksheet.Range("A1").CopyFromRecordset rstRecordset
        Set wksWorksheet = Nothing

        ' Close record set
        Set rstRecordset = Nothing

    ' Close record set
    If Not rstRecordset Is Nothing Then
        Set rstRecordset = Nothing
    End If

    ' Close spreadsheet
    Set wksWorksheet = Nothing
    If Not wkbWorkbook Is Nothing Then
        wkbWorkbook.Close True  ' Save changes
    End If
    If Not appExcel Is Nothing Then
        Set appExcel = Nothing
    End If

    ' Exit
    Exit Sub

' Handle export errors

    ' Report error
    MsgBox "Cannot export data." & vbCr & _
           Err.Description, _
           vbExclamation + vbOKOnly + vbDefaultButton1
    Resume TidyUp

End Sub

Open in new window

TCristianoAuthor Commented:
You are the Experts for a reason. I'm a bit rusty on this but your help certainly jogged my memory.  These quick thorough repsonses is what makes Expert Exchange the best resource.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.