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!
Todd.

TCristianoAsked:
Who is Participating?
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPConnect With a Mentor Commented:
Did you set a VBA reference to Excel?


0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPConnect With a Mentor Commented:
Open the VBA editor alt-F11

 VBA Tool References
then select Excel

 Excel Object
0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPConnect With a Mentor Commented:
Note:
The code you posted using Early Binding so you have to set the reference
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
JezWaltersConnect With a Mentor Commented:
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?
0
 
JezWaltersConnect With a Mentor Commented:
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
        rstRecordset.Close
        Set rstRecordset = Nothing
    Next

    ' Close record set
TidyUp:
    If Not rstRecordset Is Nothing Then
        rstRecordset.Close
        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
        appExcel.Quit
        Set appExcel = Nothing
    End If

    ' Exit
    Exit Sub

' Handle export errors
ExportError:

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

End Sub

Open in new window

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.