"Subscript out of Range" error when testing for Worksheets

krissz

Board Regular
Joined
Feb 21, 2010
Messages
95
This relates to a project which uses multiple sheets, named SL 1, SL 2, ..., SL 99, SL 100, etc. This is a contiguous sequence.
A section of code is supposed to scan through all the sheets to identify the final sheet, whereupon other actions will occur. This fails with "Run Time Error '9'" on the line "Set Ws_Sheet = Sheets("SL " & Ws_No)".
Please, what have I done wrong ?
The full code is:

Code:
Sub Check_PL_Ws()
      ' Check whether a PL xx Sheet exists & whether it is populated
      
      Dim Ws_No As Integer
      Dim Ws_Nam As String
      Dim Ws_Exist As Boolean
      Dim Ws_Sheet As Worksheets
      
      Ws_Exist = True
      Ws_No = 1
      
      Do While Ws_Exist
            
            [I][COLOR=Red]Set Ws_Sheet = Sheets("SL " & Ws_No)[/COLOR][/I]
            
            ' Does not exist
            If Ws_Sheet Is Nothing Then
            
                  Ws_Nam = "SL " & Ws_No
                  Exit Sub
            
           ' Exists
            Else
            
                  Ws_Sheet.Activate             ' For test purposes only
                  Ws_No = Ws_No + 1
            End If
            
      Loop
      
      MsgBox "Sheet SL " & Ws_No & " does not exist"
      
End Sub

This is not new code - I have found similar examples on various sites.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Dim Ws_Sheet As Worksheets

should be

Dim Ws_Sheet As Worksheet

Use this function to check whether a worksheet exists:

Code:
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean
    On Error Resume Next
    WorksheetExists = (Sheets(WorksheetName).Name <> "")
    On Error GoTo 0
End Function
Or, to determine the name of the last worksheet, try:

Sheets(Sheets.Count).Name
 
Upvote 0
John
Thank you for your help. I have now resolved it - it was finger & brain trouble. My sheets are named "PL xx" while the code was looking for sheets named "SL xx" !?
Your abbreviated code works perfectly & does exactly what I need.
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top