VBA Macro help - Year from Date

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
Hello
I adapted some code from a previous posting of mine to also look at the date column and fill in the year column.
http://www.mrexcel.com/forum/showthread.php?t=552135

Credit goes to HOTPEPPER who helped me set up the Month part of this script.

However, for my data purposes I need the 2 digit year. Eg 11 instead of 2011. Any ideas how I might achieve this?
I put the code below~

Code:
Dim tc As Range, yc As Range, y As Long
Set tc = Range("1:1").Find("Date", , , xlWhole)
If tc Is Nothing Then MsgBox "Date Column not Found", vbExclamation: Exit Sub
Set yc = Range("1:1").Find("Year", , , xlWhole)
If tc Is Nothing Then MsgBox "Year Column not Found", vbExclamation: Exit Sub
y = Cells(Rows.Count, tc.Column).End(xlUp).Row
With yc.Offset(1).Resize(y - 1)
    .Formula = "=YEAR(" & tc.Range("A2").Address(0, 0) & ")"
    .Value = .Value
End With
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try

Code:
.Formula = "=TEXT(YEAR(" & tc.Range("A2").Address(0, 0) & ",""yy"")"
 
Upvote 0
Assuming the above code works as desired with the exception of wanting 2 digit year...

Try changing
.Formula = "=YEAR(" & tc.Range("A2").Address(0, 0) & ")"
to
.Formula = "=RIGHT(YEAR(" & tc.Range("A2").Address(0, 0) & "),2)+0"
 
Upvote 0
Try

Code:
.Formula = "=TEXT(YEAR(" & tc.Range("A2").Address(0, 0) & ",""yy"")"


Hey! Thanks for both of your help, here are my results when running it with my other macros.

VoG's code
I'm using excel 2003 and received a Run-Time error '1004' Application-defined or Object-Defined Error


jonmo1's code

This worked! Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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