Macro to change 2 digit date to 4 digits

  • Thread starter Thread starter SUE HARGRAVE
  • Start date Start date
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.
 
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.
 
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
 
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.
 
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
 
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
 
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.
 
Back
Top