If/Then in a Macro - Changing 2 digit year into 4 digit year.

Breanna

New Member
Joined
Sep 19, 2013
Messages
13
Hi!!

I need a macro where I can highlight a column and change all of the 2 digit years to a 4 digit year (actually, some of the 2 digit years are only 1 digit, e.g. "9" instead of "09").

I have an if then statement that I can use in the column after it, but I'd rather change the actual numbers in the original column, rather than adding another column (and having to keep the original, too).

The statement I had was:
=if(A1>=50,1900+A1,2000+A1)

I just copy dragged down to get the cell numbers for the rest of the column... but using A1 was just for an example, here, it's not necessarily going to be in that column. It needs to be just whichever cells I select.

It seems like it should be pretty simple, but I don't know how to word it in a macro.


Starting Column Example:
12
13
14
99
11
9
8
6
14
97
0
1
2
3

<tbody>
</tbody>

Ending Column Example:
2012
2013
2014
1999
2011
2009
2008
2006
2014
1997
2000
2001
2002
2003

<tbody>
</tbody>


Any help would be appreciated. :)

Thank you!!!

Edit: for some reason I can't get rid of the borders... weird, lol.
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Public Sub convertYears()    
    Dim r As Range
    For Each r In Selection
        If r.Value >= 50 Then
            r.Value = 1900 + r.Value
        Else
            r.Value = 2000 + r.Value
        End If
    Next
End Sub
 
Upvote 0
Breanna,

Sample raw data:


Excel 2007
ABCDEFGH
112
213
314
4991213149911
5119861497
69
78
8601
91423
1097
110
121
132
143
15
Sheet1


Each time you select one of the colored cell groups, and, run the macro, the overall results would look like this:


Excel 2007
ABCDEFGH
12012
22013
32014
4199920122013201419992011
5201120092008200620141997
62009
72008
8200620002001
9201420022003
101997
112000
122001
132002
142003
15
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub UpdateYear()
' hiker95, 07/03/2014, ME789246
Dim c As Range
Application.ScreenUpdating = False
For Each c In Selection
  If c >= 50 Then
    c = 1900 + c
  Else
    c = 2000 + c
  End If
Next c
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the UpdateYear macro.
 
Upvote 0
Breanna,

Thanks for the macro Hiker!

Both of the ones posted work, but I'm going to use yours

Thanks for the above Private Message, and, for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Here is another macro you might find interesting (it uses no loops). One reason for recommending it is that it uses the 1900/2000 century breakpoint year set in your computer's regional setting rather than picking an arbitrary 50 year breakpoint.
Code:
Sub ExpandToFourDigitYears()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate("IF(LEN(" & Addr & "),YEAR(0+(""1/1/""&" & Addr & ")),"""")")
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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