extrapolate year from date

bank

New Member
Joined
Sep 2, 2010
Messages
25
I have a spreadsheet with columns 6/30/2010, 6/30/2011, 6/30/2012 etc.

I want to create new sheets within the same workbook with only the year as the label, so the resulting sheet labels will be 2010,2011,2012.

I don't want to hard code the year as the label, because the dates may change. I just want to be able to reference the year and use that as the label. Below is the code I have right now, but it labels the tabs "Jun-10", "Jun-11" etc.


HTML:
Public Sub add_four_sheets()
Dim name1, name2, name3, name4 As String
Dim StrSrcSheet As String
 
StrSrcSheet = "Maintenance"
' add and rename four sheets
    name1 = Worksheets("Maintenance").Range("Y6").Text
    name2 = Worksheets("Maintenance").Range("AL6").Text
    name3 = Worksheets("Maintenance").Range("AY6").Text
    name4 = Worksheets("Maintenance").Range("BL6").Text
 
 'Add FYE10
    Worksheets.Add After:=Worksheets(Sheets.Count)
    ActiveSheet.Name = name1
    Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy Worksheets(name1).Range("A1")
    Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy
        Worksheets(name1).Range("A1").PasteSpecial xlPasteColumnWidths
 
'Add FYE11
    Worksheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = name2
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy Worksheets(name2).Range("A1")
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy
        Worksheets(name2).Range("A1").PasteSpecial xlPasteColumnWidths
        
'Add FYE12
     Worksheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = name3
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy Worksheets(name3).Range("A1")
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy
        Worksheets(name3).Range("A1").PasteSpecial xlPasteColumnWidths

'Add FYE13
     Worksheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = name4
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy Worksheets(name4).Range("A1")
        Worksheets(StrSrcSheet).Range("A1:A6").EntireRow.Copy
        Worksheets(name4).Range("A1").PasteSpecial xlPasteColumnWidths
End Sub()
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Does this work for you? It assumes the values in those four cells are Excel dates.
Code:
Public Sub AddFourSheets()
    Dim wks         As Worksheet
    Dim vsRng       As Variant
 
    Set wks = Worksheets("Maintenance")
 
    For Each vsRng In Array("Y6", "AL6", "AY6", "BL6")
        Worksheets.Add After:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = Format(wks.Range(vsRng).Value, "yyyy")
        wks.Rows("1:6").Copy
        ActiveSheet.Range("A1").PasteSpecial
        ActiveSheet.Range("A1").PasteSpecial xlPasteColumnWidths
    Next vsRng
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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