I have code that I've created that is super close to working. It essentially loops thru a record set creating query defs and then transfers to Excel and creates a file. Each loop creates the query def, deletes def, and outputs an Excel file. After Excel file is created I open the Excel file and set auto width and background color for header. I then loop through the file to highlight any "Missing" or "Mismapped" data. That works fine. Its the last loop where I am attempting to test each column (minus the header) for any data, if no data I want it to hide the column. Here the problem occurs on the 2nd file, the first file it works fine on, so I know I'm really close. Any help would be appreciated, I think it is a variable that may need to be reset or placed in a different section of code...
Error Message: Run Time Error '1004', Method Range of Object Failed
'lRealLastRow = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByRows, xlPrevious).Row ---- This line fails below, near the bottom of code
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTemp As String
Dim STRsql As String
Dim QueryName As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qrySP-CW-Caseworker")
Dim txtMonth As String
Dim FileName As String
txtMonth = DLookup("[maxMonth]", "qryMaxMonth")
Do Until rs.EOF
strTemp = rs![case worker]
Debug.Print strTemp
Dim qdf As DAO.QueryDef
QueryName = strTemp
STRsql = "SELECT * FROM [tblSP-CW_Main] WHERE (((case worker)='" & (strTemp) & "'));"
Debug.Print STRsql
Debug.Print QueryName
Set qdf = CurrentDb.CreateQueryDef(QueryName, "SELECT * FROM [tblSP-CW_Main] WHERE (([cap worker])='" & (strTemp) & "');")
FileName = "O:\Programs\Support Services\Reports\Data Quality\" & txtMonth & "\" & txtMonth & "_" & QueryName & ".xlsx"
Debug.Print FileName
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, QueryName, FileName, True
DoCmd.TransferSpreadsheet acExport, , QueryName, FileName, True
CurrentDb.QueryDefs.Delete QueryName
Set qdf = Nothing
Dim objExcel_App2 As Excel.Application ' <--
Dim xlsExcel_Sheet2 As Excel.Worksheet
Dim xlrMyRange2 As Excel.Range
Dim Cloop As Excel.Range
Dim Ecolumn As Excel.Range
Set objExcel_App2 = New Excel.Application ' <--
objExcel_App2.Visible = False
objExcel_App2.Workbooks.Open FileName:="O:\Programs\Support Services\Reports\Data Quality\" & txtMonth & "\" & txtMonth & "_" & QueryName & ".xlsx"
If Val(objExcel_App2.Application.Version) >= 8 Then
Set xlsExcel_Sheet2 = objExcel_App2.Worksheets(1) ' <--
Else
End If
Set xlrMyRange2 = xlsExcel_Sheet2.Range("A1:W1")
xlrMyRange2.Font.Bold = True
xlrMyRange2.Interior.Color = RGB(204, 229, 255)
objExcel_App2.Cells.Columns.AutoFit
Set Cloop = xlsExcel_Sheet2.Range("A2:W30")
With Cloop
For Each Cell In Cloop
If Cell.Value = "Missing" Or Cell.Value = "Mismapped" Then
Cell.Interior.Color = RGB(255, 255, 0)
Cell.Font.Bold = True
End If
Next Cell
End With
Dim lRealLastRow, lRealLastCol As Long
Dim i As Long
i = 1 'also failed when not set, but the same on 2nd Excel, 1st Excel file it works
[highlight #FCE94F]lRealLastRow = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByRows, xlPrevious).Row[/highlight]
lRealLastCol = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByColumns, xlPrevious).Column
For i = 1 To lRealLastCol
' If lRealLastRow - WorksheetFunction.CountBlank(Intersect(Columns(i), ActiveSheet.UsedRange)) <= 1 Then
objExcel_App2.Cells.Columns(i).EntireColumn.Hidden = True
Else
End If
' Next i
objExcel_App2.Application.ActiveWorkbook.Save
objExcel_App2.Application.ActiveWorkbook.Close
objExcel_App2.Quit
Set objExcel_App2 = Nothing
rs.MoveNext
Loop
rs.Close
MsgBox ("done")
Error Message: Run Time Error '1004', Method Range of Object Failed
'lRealLastRow = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByRows, xlPrevious).Row ---- This line fails below, near the bottom of code
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTemp As String
Dim STRsql As String
Dim QueryName As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qrySP-CW-Caseworker")
Dim txtMonth As String
Dim FileName As String
txtMonth = DLookup("[maxMonth]", "qryMaxMonth")
Do Until rs.EOF
strTemp = rs![case worker]
Debug.Print strTemp
Dim qdf As DAO.QueryDef
QueryName = strTemp
STRsql = "SELECT * FROM [tblSP-CW_Main] WHERE (((case worker)='" & (strTemp) & "'));"
Debug.Print STRsql
Debug.Print QueryName
Set qdf = CurrentDb.CreateQueryDef(QueryName, "SELECT * FROM [tblSP-CW_Main] WHERE (([cap worker])='" & (strTemp) & "');")
FileName = "O:\Programs\Support Services\Reports\Data Quality\" & txtMonth & "\" & txtMonth & "_" & QueryName & ".xlsx"
Debug.Print FileName
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, QueryName, FileName, True
DoCmd.TransferSpreadsheet acExport, , QueryName, FileName, True
CurrentDb.QueryDefs.Delete QueryName
Set qdf = Nothing
Dim objExcel_App2 As Excel.Application ' <--
Dim xlsExcel_Sheet2 As Excel.Worksheet
Dim xlrMyRange2 As Excel.Range
Dim Cloop As Excel.Range
Dim Ecolumn As Excel.Range
Set objExcel_App2 = New Excel.Application ' <--
objExcel_App2.Visible = False
objExcel_App2.Workbooks.Open FileName:="O:\Programs\Support Services\Reports\Data Quality\" & txtMonth & "\" & txtMonth & "_" & QueryName & ".xlsx"
If Val(objExcel_App2.Application.Version) >= 8 Then
Set xlsExcel_Sheet2 = objExcel_App2.Worksheets(1) ' <--
Else
End If
Set xlrMyRange2 = xlsExcel_Sheet2.Range("A1:W1")
xlrMyRange2.Font.Bold = True
xlrMyRange2.Interior.Color = RGB(204, 229, 255)
objExcel_App2.Cells.Columns.AutoFit
Set Cloop = xlsExcel_Sheet2.Range("A2:W30")
With Cloop
For Each Cell In Cloop
If Cell.Value = "Missing" Or Cell.Value = "Mismapped" Then
Cell.Interior.Color = RGB(255, 255, 0)
Cell.Font.Bold = True
End If
Next Cell
End With
Dim lRealLastRow, lRealLastCol As Long
Dim i As Long
i = 1 'also failed when not set, but the same on 2nd Excel, 1st Excel file it works
[highlight #FCE94F]lRealLastRow = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByRows, xlPrevious).Row[/highlight]
lRealLastCol = objExcel_App2.Cells.Find("*", Range("A1"), xlValues, , xlByColumns, xlPrevious).Column
For i = 1 To lRealLastCol
' If lRealLastRow - WorksheetFunction.CountBlank(Intersect(Columns(i), ActiveSheet.UsedRange)) <= 1 Then
objExcel_App2.Cells.Columns(i).EntireColumn.Hidden = True
Else
End If
' Next i
objExcel_App2.Application.ActiveWorkbook.Save
objExcel_App2.Application.ActiveWorkbook.Close
objExcel_App2.Quit
Set objExcel_App2 = Nothing
rs.MoveNext
Loop
rs.Close
MsgBox ("done")