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
 
your method still creates the sheet just doesn't rename it if the sheet name exists.
Oops, my bad, trying to keep it minimal I cut it a bit too short
VBA Code:
Dim wsName As String
wsName = Format(DateAdd("m", -1, Date), "mmm_yy")
On Error Resume Next
    If Worksheets(wsName).Name = wsName Then Exit Sub
On Error GoTo 0
    ThisWorkbook.Sheets.Add(After:=.Sheets("IC DATA")).Name = wsName
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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