ACCESS: Macro to export query results to specific tab in Excel spreadsheet, whilst overwriting existing contents.

Anonymous
2011-01-18T20:33:05+00:00

Hi,

I need to write a macro for access, that will export the contents of a query to a specific tab on an excel spreadsheet, whilst overwriting the exiting contents. Is this possible?

I have tried various ways, but it always wants to overwrite the excel file altogether!

Thank you.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

47 answers

Sort by: Most helpful
  1. Anonymous
    2011-11-03T19:01:22+00:00

    That fixed the issue, works perfect now.  Thanks for you help.  Below is a copy of the working code.

    Public Function SendTQ2XLWbSheet(strTQName As String, strSheetName As String, strFilePath As String)

    ' strTQName is the name of the table or query you want to send to Excel

    ' strSheetName is the name of the sheet you want to send it to

    ' strFilePath is the name and path of the file you want to send this data into.

        Dim rst As DAO.Recordset

        Dim ApXL As Object

        Dim xlWBk As Object

        Dim xlWSh As Object

        Dim fld As Field

        Dim blnSaveAs As Boolean

        Dim i As Long

        Dim strPath As String

        Const xlCenter As Long = -4108

        Const xlBottom As Long = -4107

        On Error GoTo err_handler

        strPath = strFilePath

        Set rst = CurrentDb.OpenRecordset(strTQName)

        If rst.RecordCount = 0 Then

            MsgBox "There are no records to export"

            Exit Function

        End If

        Set ApXL = CreateObject("Excel.Application")

        Set xlWBk = ApXL.Workbooks.Open(strPath)

        ApXL.Visible = True

        Set xlWSh = xlWBk.Worksheets(strSheetName)

        For i = 0 To rst.Fields.Count - 1

            xlWSh.Cells(1, i + 1) = rst.Fields(i).Name

        Next i

        rst.MoveFirst

        xlWSh.Range("A2").CopyFromRecordset rst

        ' This is included to show some of what you can do about formatting.

        ' You can comment out or delete

        ' any of this that you don't want to use in your own export.

        With xlWSh.Range("1:1").Font

            .Name = "Arial"

            .Size = 12

            .Strikethrough = False

            .Superscript = False

            .Subscript = False

            .OutlineFont = False

            .Shadow = False

            .Bold = True

        End With

        With xlWSh.Range("1:1")

            .HorizontalAlignment = xlCenter

            .VerticalAlignment = xlBottom

            .WrapText = False

            .Orientation = 0

            .AddIndent = False

            .IndentLevel = 0

            .ShrinkToFit = False

            .MergeCells = False

        End With

        ' does the "autofit" for all columns

        xlWSh.Cells.EntireColumn.AutoFit

        rst.Close

        Set rst = Nothing

        ApXL.DisplayAlerts = False

        If Dir(strFilePath) = "" Then

            xlWBk.SaveAs strFilePath

        Else

            xlWBk.Save

        End If

        xlWBk.Close

        ApXL.DisplayAlerts = True

        ApXL.Quit

        Set ApXL = Nothing

        Exit Function

    err_handler:

        DoCmd.SetWarnings True

        MsgBox Err.Description, vbExclamation, Err.Number

    End Function

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.7K Reputation points MVP Volunteer Moderator
    2011-11-03T16:27:08+00:00

    Yes, I see now - my bad. Sorry!

    Change

        For i = 0 To rst.Fields.Count - 1

            xlWSh.Cells(1, i + 1) = fld.Name

        Next i

    to

        For i = 0 To rst.Fields.Count - 1

            xlWSh.Cells(1, i + 1) = rst.Fields(i).Name

        Next i

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-11-03T15:37:27+00:00

    I can't get this work in debug, but I believe it is in one of the lines below.  As it opens the workbook to the specified sheet name then stops.

    For i = 0 To rst.Fields.Count - 1

            xlWSh.Cells(1, i + 1) = fld.Name

        Next i

        rst.MoveFirst

        xlWSh.Range("A2").CopyFromRecordset rst

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.7K Reputation points MVP Volunteer Moderator
    2011-11-02T22:43:09+00:00

    On which line do you get the error?

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2011-11-02T22:09:28+00:00

    I copied in the new code above, it opened the workbook to the specified sheet name, but then stopped with a new error. 91 Object variable or With block variable not set .  After getting the error I opened the target workbook selected the target sheet and saved.  When I ran it again it opened the workbook, but didn't copy any data and gave me the save error.

    Was this answer helpful?

    0 comments No comments