I'm trying to loop through all the worksheets in an Excel file using Access VBA.
The subroutine needs to select the first row and set the RowHeight in each worksheet.
I'm using a string variable to call the subroutine and passing the worksheet name to it. It works the first time but the next time I get
"Select method of Range Class Failed"
I tried moving the variable declarations around, changing where I open Excel (it opens twice if I put it in the subroutine) and doing it as function instead of a subroutine.
'My object and worksheet variables are declared at the top of the object:
Dim objExcel As Object
Dim wks As Worksheet
Dim wkb As Workbook
'I'm opening Excel and setting the workbook object in a subroutine:
Set objExcel = CreateObject("Excel.Application") 'Excel is invoked 01
Set wkb = objExcel.Workbooks.Open(strOutputPathAndFileName)
objExcel.Application.Visible = True
'Then calling the subroutine to set the row height and wrap text property
FirstRowHeightAndWrap ("ChangeTracking")
FirstRowHeightAndWrap ("FivePCalcsThisPPE")
'Here's the function
Function FirstRowHeightAndWrap(strSheetName As String)
Set wks = wkb.Sheets(strSheetName)
With wks
.Rows(1).Select
.Rows(1).RowHeight = 28
.Rows(1).WrapText = True
End With
End Function
It's something about setting the wks variable in a different place than the objExcel and wkb variables, clearly, 'cuz it works if I do it all in one place.