CFMI
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.Applic ation")
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("Q fltData")
Set Sheet = xlObj.activeworkbook.Works heets("Tax ")
Sheet.Range("A2").CopyFrom Recordset rs 'copy the data
Set rs = CurrentDb.OpenRecordset("Q fltDataTot als")
Set Sheet = xlObj.activeworkbook.Works heets("Fin ance")
Sheet.Range("A2").CopyFrom Recordset rs
'continue here
'save the excel file
xlObj.activeworkbook.SaveA s "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
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.Applic
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("Q
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A2").CopyFrom
Set rs = CurrentDb.OpenRecordset("Q
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A2").CopyFrom
'continue here
'save the excel file
xlObj.activeworkbook.SaveA
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent - Thank you!
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:
Open in new window
becomes:
Open in new window
Patrick