codeEnthusiast
codeEnthusiast

Reputation: 321

Go to the last worksheet whose name contains string

I have a bunch of dynamically created worksheets in my workbook that all contain the phrase "Roster" in their name. I am trying to activate the last of these worksheets, but I can't figure out how to get the last sheet. Using this:

if Worksheets(i).Name Like "*Roster*" Then

I can loop through the worksheets and perform some actions on all the sheets that contain that substring in their names, but how do I just perform actions on only the last of these?

Upvotes: 0

Views: 79

Answers (1)

BigBen
BigBen

Reputation: 50162

Perhaps loop over the worksheets backwards and exit the loop after the first match:

Dim i As Long
For i = Worksheets.Count to 1 Step -1
    If Worksheets(i).Name Like "*Roster*" Then
        ' do the stuff
        Exit For
    End If
Next

Upvotes: 4

Related Questions