Link to home
Start Free TrialLog in
Avatar of CFMI
CFMIFlag for United States of America

asked on

MS Access VBA to export queries into worksheets in the same workbook

Hello Experts,

I am trying to delete spreadsheet rows but the below code has an invalid reference in the
command - With .Cells(Lrow, "A").  I used a template to transfer formats and overwrite the fields with two queries but I want to delete the rows displaying the template records.  Can you please help?

Private Sub ExportWorkbook_Click()
Dim rs As DAO.Recordset
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlWB As Excel.Workbook
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
Firstrow = 2
Lastrow = 10000

xlFile = "H:\Master-template.xlsx"
    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open xlFile
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("QfltData")
    Set Sheet = xlObj.activeworkbook.Worksheets("Tax")
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
   
    Set rs = CurrentDb.OpenRecordset("QfltDataTotals")
    Set Sheet = xlObj.activeworkbook.Worksheets("Finance")
    Sheet.Range("A2").CopyFromRecordset rs

'continue here
    'save the excel file
    xlObj.activeworkbook.SaveAs "H:\MyExcel.xlsx"
   
    Set Sheet = Nothing
    xlObj.Quit
    Set xlObj = Nothing
   
For Lrow = Lastrow To Firstrow Step -1
            'We check the values in the A column in this example
            With .Cells(Lrow, "A")
                If Not IsError(.Value) Then
                    If .Value = "175001" Then .EntireRow.Delete
                    'This will delete each row with the Value "ron"
                    'in Column A, case sensitive.
                End If
            End With
        Next Lrow
    End With

MsgBox "The spreadsheet is ready!!!"
   
   
End Sub
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

CFMI,

1) Looks like you quit Excel and start destroying the Excel objects before getting into the For/Next loop, in which you refer to Excel objects.  Why?

2) Instead of deleting the rows after they get to Excel, why not simply modify your query to exclude those rows before they even hit Excel?

3) If you really want to use a template, which is a fine idea, then create a real template (file type .XLTX), and instead of reopening and then saving that XLSX file

4) You should maintain an object reference to your workbook to ensure maximum control.  Thus, this code:

    xlFile = "H:\Master-template.xlsx"
    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open xlFile
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("QfltData")
    Set Sheet = xlObj.activeworkbook.Worksheets("Tax")
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
    
    Set rs = CurrentDb.OpenRecordset("QfltDataTotals")
    Set Sheet = xlObj.activeworkbook.Worksheets("Finance")
    Sheet.Range("A2").CopyFromRecordset rs

    'continue here
    'save the excel file
    xlObj.activeworkbook.SaveAs "H:\MyExcel.xlsx"

Open in new window


becomes:

    xlFile = "H:\Master-template.xltx"
    Set xlObj = CreateObject("Excel.Application")
    Set xlBook = xlObj.Workbooks.Add(xlFile)
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("QfltData")
    Set Sheet = xlBook.Worksheets("Tax")
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
    
    Set rs = CurrentDb.OpenRecordset("QfltDataTotals")
    Set Sheet = xlBook.Worksheets("Finance")
    Sheet.Range("A2").CopyFromRecordset rs

'continue here
    'save the excel file
    xlBook.SaveAs "H:\MyExcel.xlsx"

Open in new window



Patrick
Avatar of CFMI

ASKER

Hi Patrick,

It appears that within VBA you can refer to a template and extract records using this format.  Is this true?  
I need to extract two different queries to be inserted into the same workbook but in their own worksheet.  First, I began with DoCmd.TransferSpreadsheet which extracts into specific worksheets but without formatting.  Second, I realized that DoCmd.OutputTo extracts with formatting but into only one worksheet.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFMI

ASKER

Excellent - Thank you!