Home > front end >  Go to the last worksheet whose name contains string
Go to the last worksheet whose name contains string

Time:11-18

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?

CodePudding user response:

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
  • Related