VBA code to format Year(NOW) to YY

dannyh

Board Regular
Joined
Oct 15, 2007
Messages
156
Hi I am using the following code to rename a new sheet that gets created when the workbook is opened.

Can anyone help me add code to format the Year as only 2 digits not 4, thanks.


VBA Code:
Dim iMonth As Integer, sMonth As String
Dim sYear As String

iYear = Year(Now)
iMonth = Month(Now) - 1
Select Case iMonth
Case 1
sMonth = "Jan"
Case 2
sMonth = "Feb"
Case 3
sMonth = "Mar"
Case 4
sMonth = "Apr"
Case 5
sMonth = "May"
Case 6
sMonth = "Jun"
Case 7
sMonth = "Jul"
Case 8
sMonth = "Aug"
Case 9
sMonth = "Sep"
Case 10
sMonth = "Oct"
Case 11
sMonth = "Nov"
Case 10
sMonth = "Dec"
End Select
Worksheets(1).Name = sMonth & "_" & iYear

Thanks Dan
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
That's a lot of code to rename a sheet based on the date, try this single line instead.
VBA Code:
Worksheets(1).Name = Format(Date,"mmm_yy")
 
Upvote 0
If you want last month, then try
VBA Code:
   Worksheets(1).Name = Format(DateAdd("m", -1, Date), "mmm_yy")
 
Upvote 0
Good catch, Fluff. I missed the month adjustment.
 
Upvote 0
Perfect guys thanks for the quick responses, can we add in an error check so if the sheet already exists it ignores it ?

using IF I guess ?

VBA Code:
With ThisWorkbook
        .Sheets.Add(After:=.Sheets("IC DATA")).Name = Format(DateAdd("m", -1, Date), "mmm_yy")
    End With

Thanks Dan
 
Upvote 0
How about
VBA Code:
   Dim ShtName As String
   ShtName = Format(DateAdd("m", -1, Date), "mmm_yy")
   If Evaluate("isref('" & ShtName & "'!A1)") Then Exit Sub
   Sheets.Add(, Sheets("IC DATA")).Name = ShtName
 
Upvote 0
can we add in an error check so if the sheet already exists it ignores it ?
If you only want to skip the sheet creation then you could use
VBA Code:
On Error Resume Next
With ThisWorkbook
        .Sheets.Add(After:=.Sheets("IC DATA")).Name = Format(DateAdd("m", -1, Date), "mmm_yy")
End With
On Error Goto 0
 
Upvote 0
How about
VBA Code:
   Dim ShtName As String
   ShtName = Format(DateAdd("m", -1, Date), "mmm_yy")
   If Evaluate("isref('" & ShtName & "'!A1)") Then Exit Sub
   Sheets.Add(, Sheets("IC DATA")).Name = ShtName

Great Fluff, spot on.
 
Upvote 0
If you only want to skip the sheet creation then you could use
VBA Code:
On Error Resume Next
With ThisWorkbook
        .Sheets.Add(After:=.Sheets("IC DATA")).Name = Format(DateAdd("m", -1, Date), "mmm_yy")
End With
On Error Goto 0

Thanks Jason, your method still creates the sheet just doesn't rename it if the sheet name exists. Fluff's response actually stops the sheet creation which is what I needed.

Thanks for taking the time to answer.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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