Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2025
    Posts
    1

    Post How can I append the output from multiple VBA codes into a single Excel file without overwriting the

    I have two separate VBA codes:


    1. One exports the results of an SQL query to an Excel file.




    2. The other exports data generated within VBA to an Excel file.






    I need to append both outputs into a single Excel file without overwriting the existing data. The new data should be added below the existing content, ensuring both sets of results are preserved.




    Can you provide a VBA solution to achieve this?

  2. #2
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    to append data, use Excel automation.
    create this Public Sub to a Module, then pass the recordset that you want to apppend
    and the Path+Name of the workbook, and the name of the sheet to where the
    data will be appended:

    Code:
    Sub AppendToExcel(ByRef rst As dao.Recordset, ByVal WorkBook As String, Optional ByVal Sheet As String = "Sheet1")
        Dim xlApp As Object
        Dim xlWorkbook As Object
        Dim xlSheet As Object
        Dim LastRow As Long
    
    
        ' Open Excel
        Set xlApp = CreateObject("Excel.Application")
        Set xlWorkbook = xlApp.Workbooks.Open(WorkBook)
        Set xlSheet = xlWorkbook.Sheets(Sheet)
    
    
        ' Find last used row
        LastRow = xlSheet.Cells(xlSheet.Rows.Count, "A").End(-4162).Row + 1 ' xlUp
        
        xlSheet.range("A" & LastRow).CopyFromRecordset rst
        
        xlWorkbook.Close True
        Set xlSheet = Nothing
        Set xlWorkbook = Nothing
        xlApp.Quit
        Set xlApp = Nothing
        
    End Sub

  3. #3
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    That's OK if column A is the deepest. If some other column is the deepest/longest then it will over-write in those rows, no? I think I would use Find instead. Sample:
    lRow = .Cells.Find(what:="*", After:=Range("I5"), LookAt:=xlPart, LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).row

    If I'm not mistaken, using Find in code affects the application's Find parameters so it's best to specify most if not all of them each time. I might be confusing that with something else so I suggest reading up on the method.

    Also, pretty sure one needs a reference to Excel library in order to use automation of Excel from within Access and I didn't see that mentioned.
    EDIT - that is contained in a With block, which is why the reference starts with .Cells
    Last edited by Micron; 02-04-2025 at 10:07 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,566
    I use this
    Code:
    Public Function GetLastRow(pstrSheet As String, Optional pstrColumn As String) As Long
    ' Return last used row for sheet and column passed in
    Dim lngLastRow As Long
    Dim sht As Worksheet
    
    
    Set sht = Sheets(pstrSheet)
    If pstrColumn = "" Then pstrColumn = "A"
    
    
    lngLastRow = sht.Cells(ActiveSheet.Rows.Count, pstrColumn).End(xlUp).Row
    GetLastRow = lngLastRow
    Set sht = Nothing
    
    
    End Function
    so I can specify which column to check.

    Eg: lngLastRowCopy = GetLastRow(ActiveSheet.Name, strColName1)

    I have it in my Personal.xlsb file and a reference to it in any workbook I need it in.
    Could just copy and paste as well.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 11 Office 365
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    Use Power Query (Get and Transpose Data found on the Data Tab) to bring each into the PQ editor. Once in the Editor, you can join or append each to the other within the UI. No coding required. Once completed, close and load to Native Excel.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 11-24-2019, 08:24 AM
  2. Replies: 3
    Last Post: 08-07-2019, 02:57 PM
  3. Replies: 3
    Last Post: 04-02-2019, 08:36 AM
  4. Replies: 2
    Last Post: 06-25-2015, 03:56 PM
  5. Replies: 7
    Last Post: 06-05-2015, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums