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-01-19T22:04:38+00:00

    I'm sorry if I have offended you Bob, you have been very helpful I promise!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-01-19T22:02:21+00:00

    Thank you to you both!

    I did try that (I think) ... The original code was for January, so I made a new call and changed "Jan" for "Feb".

    The code exports nothing to the Feb tab, and Access ends up with an error:

    Select method of range class failed.

    It's not that I don't get it, I have tried but failed. :-(

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-01-19T21:56:40+00:00

    Oh, and I merged the two threads together since there shouldn't have been another one to begin with.


    Bob Larson, Former Access MVP (2008-2010) http://www.btabdevelopment.com (free Access tools, tutorials, and samples)

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-01-19T21:53:37+00:00

    You don't copy the module again.  As Scott said, you call it from wherever you want.  So, if you want multiple queries in one click of a button you would use:

    Private Sub Image57_Click()

    Call SendTQ2XLWbSheet("JP Completions Jan", "Jan", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    Call SendTQ2XLWbSheet("JP Completions Feb", "Feb", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    Call SendTQ2XLWbSheet("JP Completions Mar", "Mar", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    Call SendTQ2XLWbSheet("JP Completions Apr", "Apr", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    Call SendTQ2XLWbSheet("JP Completions May", "May", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    End Sub

    and so on.  Do you get it now?


    Bob Larson, Former Access MVP (2008-2010) http://www.btabdevelopment.com (free Access tools, tutorials, and samples)

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2011-01-19T21:49:29+00:00

    This is what Bob gave me, and it works, but only for one query to spreadsheet. When I copy both parts, and amend the call, it says the module is ambiguous.

    CALL AS FOLLOWS:

    Private Sub Image57_Click()

    Call SendTQ2XLWbSheet("JP Completions Jan", "Jan", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    End Sub

    MODULE AS FOLLOWS:

    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 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)

        Set ApXL = CreateObject("Excel.Application")

        Set xlWBk = ApXL.Workbooks.Open(strPath)

        ApXL.Visible = True

        Set xlWSh = xlWBk.Worksheets(strSheetName)

        xlWSh.Range("A1").Select

        If rst.RecordCount = 0 Then

        MsgBox "There are no records to export"

        Exit Function

    End If

        For Each fld In rst.Fields

            ApXL.ActiveCell = fld.Name

            ApXL.ActiveCell.Offset(0, 1).Select

        Next

        rst.MoveFirst

        xlWSh.Range("A2").CopyFromRecordset rst

        xlWSh.Range("1:1").Select

        ' 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 ApXL.Selection.Font

            .Name = "Arial"

            .Size = 12

            .Strikethrough = False

            .Superscript = False

            .Subscript = False

            .OutlineFont = False

            .Shadow = False

        End With

        ApXL.Selection.Font.Bold = True

        With ApXL.Selection

            .HorizontalAlignment = xlCenter

            .VerticalAlignment = xlBottom

            .WrapText = False

            .Orientation = 0

            .AddIndent = False

            .IndentLevel = 0

            .ShrinkToFit = False

            .MergeCells = False

        End With

        ' selects all of the cells

        ApXL.ActiveSheet.Cells.Select

        ' does the "autofit" for all columns

        ApXL.ActiveSheet.Cells.EntireColumn.AutoFit

        ' selects the first cell to unselect all cells

        xlWSh.Range("A1").Select

        rst.Close

        Set rst = Nothing

        Exit Function

    err_handler:

        DoCmd.SetWarnings True

        MsgBox Err.Description, vbExclamation, Err.Number

        Exit Function

    End Function

    Was this answer helpful?

    0 comments No comments