Find worksheet with variable name

camandab

Board Regular
Joined
Feb 10, 2010
Messages
79
Hello. I have a series of templates that I'm working with. Each template will contain at least 1 worksheet for the current month of data. I need to copy the previous month's worksheet and the name will be in format "MMM YY". I can't always retrieve the last sheet as there may be a supporting worksheet from the previous month that will occupy the last sheet in the workbook so this is why I need to find the previous month using that format. Also there is a 1 month lag so if I'm working this process in the calendar month of April, I will need to find the sheet named "Feb 10", etc. Is there anyway I can code this using vba? Any help will be much appreciated!!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Also there is a 1 month lag so if I'm working this process in the calendar month of April, I will need to find the sheet named "Feb 10"
If there is a one month lag and it is currently the month of April, wouldn't you want to look at March and not February (February would be a two month lag)?

In any event, if you wanted to activate the sheet from last month, you could do so with VBA code like this:
Code:
Sub mySheetSelect()
 
    Dim mySheet As String
    
    On Error GoTo err_fix
    mySheet = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "MMM YY")
    Sheets(mySheet).Activate
    On Error GoTo 0
    
    Exit Sub
    
err_fix:
    If Err.Number = 9 Then
        MsgBox "There is no sheet named " & mySheet
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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