Hello,
I'm stuck once again. I would like Access to open an Excel file count the worksheets and then give each tab the name of the column heading in cell A1 of each worksheet. I had no problem with the code in Excel but when I tried to add it to Access I am getting an error on the WS_Count = obj.ActiveWorkbook.Worksheets.Count line. The error message is "Object variable or With block variable not set" I'm not understanding what I did wrong.
Here is the code:
I'm stuck once again. I would like Access to open an Excel file count the worksheets and then give each tab the name of the column heading in cell A1 of each worksheet. I had no problem with the code in Excel but when I tried to add it to Access I am getting an error on the WS_Count = obj.ActiveWorkbook.Worksheets.Count line. The error message is "Object variable or With block variable not set" I'm not understanding what I did wrong.
Here is the code:
Code:
'This block of code will cycle through each tab and name them
Dim WS_Count As Integer
Dim I As Integer
Dim obj As Object
Set obj = CreateObject("Excel.Application")
obj.Visible = True
' Set WS_Count equal to the number of worksheets in the active
' workbook.
[COLOR=#FF0000][B]WS_Count = obj.ActiveWorkbook.Worksheets.Count[/B][/COLOR]
' Begin the loop.
For I = 1 To WS_Count
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
obj.ActiveWorkbook.Worksheets(I).Name = obj.ActiveWorkbook.Worksheets(I).Range("A1").Value
Next I