Macro to change 2 digit date to 4 digits

S

SUE HARGRAVE

Hello,

I would like help with creating a macro that will put a four digit year in a
cell that has a 2 or 3 digit year. I download a file that shows the year as
98, 99, 100, 101, 102 but for my calculations I need the year to show as
1998, 1999, 2000, 2001, 2002. If it is not possible to have a macro change
the number in the same cell, it would be fine if the 4 digit number ended up
in the next column.

Thank you.
 
J

J.E. McGimpsey

One way:

Worksheet Function (in another column):

=YEAR(DATE(1900+A1,1,1))

another way using a macro (change in place):

Public Sub Convert2Or3DigitYears()
Dim cell As Range
For Each cell In Selection
cell.Value = Year(DateSerial(1900 + cell.Value, 1, 1))
Next cell
End Sub

Select the cells to be modified, then run the macro.
 
D

Don Guillett

Try this.Modify to suit range.

Sub chgnums()
For Each c In [a1:a5]
If c < 100 Then
c.Value = "19" & c
Else
c.Value = "20" & Right(c, 2)
End If
Next
End Sub
 
S

SUE HARGRAVE

Don,

It worked like a charm. Thank you so very much.

Sue Hargrave

Don Guillett said:
Try this.Modify to suit range.

Sub chgnums()
For Each c In [a1:a5]
If c < 100 Then
c.Value = "19" & c
Else
c.Value = "20" & Right(c, 2)
End If
Next
End Sub

SUE HARGRAVE said:
Hello,

I would like help with creating a macro that will put a four digit year
in
a
cell that has a 2 or 3 digit year. I download a file that shows the
year
as
98, 99, 100, 101, 102 but for my calculations I need the year to show as
1998, 1999, 2000, 2001, 2002. If it is not possible to have a macro change
the number in the same cell, it would be fine if the 4 digit number
ended
up
in the next column.

Thank you.
 
D

Don Guillett

Wasn't J.E's better?

SUE HARGRAVE said:
Don,

It worked like a charm. Thank you so very much.

Sue Hargrave

Don Guillett said:
Try this.Modify to suit range.

Sub chgnums()
For Each c In [a1:a5]
If c < 100 Then
c.Value = "19" & c
Else
c.Value = "20" & Right(c, 2)
End If
Next
End Sub

SUE HARGRAVE said:
Hello,

I would like help with creating a macro that will put a four digit
year
in year ended
 
S

SUE HARGRAVE

I don't understand the question... sorry.

Don Guillett said:
Wasn't J.E's better?

SUE HARGRAVE said:
Don,

It worked like a charm. Thank you so very much.

Sue Hargrave

Don Guillett said:
Try this.Modify to suit range.

Sub chgnums()
For Each c In [a1:a5]
If c < 100 Then
c.Value = "19" & c
Else
c.Value = "20" & Right(c, 2)
End If
Next
End Sub

Hello,

I would like help with creating a macro that will put a four digit
year
in
a
cell that has a 2 or 3 digit year. I download a file that shows the year
as
98, 99, 100, 101, 102 but for my calculations I need the year to
show
 
D

Don Guillett

He gave this and it's better than my offering

One way:

Worksheet Function (in another column):

=YEAR(DATE(1900+A1,1,1))

another way using a macro (change in place):

Public Sub Convert2Or3DigitYears()
Dim cell As Range
For Each cell In Selection
cell.Value = Year(DateSerial(1900 + cell.Value, 1, 1))
Next cell
End Sub


SUE HARGRAVE said:
I don't understand the question... sorry.

Don Guillett said:
Wasn't J.E's better?

SUE HARGRAVE said:
Don,

It worked like a charm. Thank you so very much.

Sue Hargrave

Try this.Modify to suit range.

Sub chgnums()
For Each c In [a1:a5]
If c < 100 Then
c.Value = "19" & c
Else
c.Value = "20" & Right(c, 2)
End If
Next
End Sub

Hello,

I would like help with creating a macro that will put a four digit year
in
a
cell that has a 2 or 3 digit year. I download a file that shows the
year
as
98, 99, 100, 101, 102 but for my calculations I need the year to
show
as
1998, 1999, 2000, 2001, 2002. If it is not possible to have a macro
change
the number in the same cell, it would be fine if the 4 digit number
ended
up
in the next column.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top