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
LVL 1
CFMIFinancial Systems AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
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
0
CFMIFinancial Systems AnalystAuthor Commented:
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
0
Patrick MatthewsCommented:
I frequently use CopyFromRecordset to copy data into workbooks based on pre-formatted Excel templates.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CFMIFinancial Systems AnalystAuthor Commented:
Excellent - Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.