Link to home
Start Free TrialLog in
Avatar of Gary Croxford
Gary CroxfordFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access VBA to Count Worksheets in a Workbook

Thank you for looking at my question,

I need to count the number of worksheets in an Excel workbook.

the code I have started with is:
        If intFileSelectedFlag > 0 Then
            Dim objXL As Object
            Dim objWB As Object
            Dim objWS As Object
            
            Set objXL = CreateObject("Excel.Application")
            Set objWB = objXL.workbooks.Open(strSource, True, True)
            Set objWS = objXL.worksheet
            
            For Each objWS In objWB.worksheets
                lngSheetCount = lngSheetCount + 1
            Next
            
            MsgBox "No. Sheets: " & lngSheetCount, vbOKOnly, "Source: " & strSource
        End If

Open in new window


The code falls over at "Set objWS = objXL.worksheet" with run-time error: 438 Object doesn't support this property or method"

How do I do this please?
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary Croxford

ASKER

Thank you
Avatar of John Smith
John Smith

It works for me as well, great job!