Subscript out of Range error trying to activate worksheets from an array

mikenelena

Board Regular
Joined
Mar 5, 2018
Messages
139
Office Version
  1. 365
Platform
  1. Windows
The following code stores various worksheets in an array and calls another module (working fine) that sends a copy of the worksheets through Outlook, to recipients. My worksheet names are numbers. They all exist, and I can see them listed in the array in the Locals window. The first sheet, 1105, shows as a variable value when I hover of the variable name.

For some reason I'm getting a subscript out of range error (ThisWorkbook.Sheets(sheetName).Activate), as if the worksheets don't exist. I've verified the names in the properties window. Why does this error persist, and how can I resolve this? Thanks in advance for any help folks can offer.

Here is the code:

VBA Code:
Sub FindMatch2()
Dim wsMain As Worksheet, wsFileData As Worksheet
Dim i As Long, j As Long
Dim matchFound As Boolean, arrCounter As Long
Dim matchDate As Date
Dim arrSheets() As Variant
Dim sheetName As Variant

Set wsMain = ThisWorkbook.Sheets("Main Menu")
Set wsFileData = ThisWorkbook.Sheets("File Data")

matchDate = wsFileData.Range("N1").Value

'Iterate through the cells in Row 2 of the Main Menu worksheet
For i = 1 To wsMain.Cells(2, Columns.Count).End(xlToLeft).Column
    If wsMain.Cells(2, i).Value = matchDate Then
        matchFound = True
        'Iterate through the cells in the found column
        For j = 3 To wsMain.Cells(Rows.Count, i).End(xlUp).Row
            If wsMain.Cells(j, i).Value > 0 Then
                'Get the sheet name from column C
                ReDim Preserve arrSheets(0 To arrCounter)
                arrSheets(arrCounter) = wsMain.Cells(j, 2).Value
                arrCounter = arrCounter + 1
            End If
        Next j
    End If

    If matchFound Then
        Exit For
    End If
Next i

''Activate each sheet in the array and run the Mail_ActiveSheet routine
If arrCounter > 0 Then
    For Each sheetName In arrSheets
        If Not ThisWorkbook.Sheets.Item(sheetName) Is Nothing Then
            ThisWorkbook.Sheets(sheetName).Activate
            Call Mail_ActiveSheet
        End If
    Next sheetName
    MsgBox "Sheets with values greater than 0: " & Join(arrSheets, ", ")
Else
    MsgBox "No sheets found with values greater than 0."
End If

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I guess it is because Sheets accepts argument as string type and you are trying to set a variable as number. Try:
VBA Code:
ThisWorkbook.Sheets(CStr(sheetName)).Activate
 
Upvote 0
Solution
Flashbond, that worked!! I can't thank you enough! I was pulling my hair out over this one today!
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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