macro to convert 19xx to 20xx?

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
782
Office Version
  1. 365
  2. 2010
when i record, it doesn't record the action

it's in column i
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hello,
Given the provided informations, the sub below should give the expected results.
VBA Code:
Sub Replace19xxTo20xx()
  Dim myVals As Variant, i As Long
  With ActiveSheet.Range("I1")
    myVals = Range(.Cells, .End(xlDown)).Value2
  End With
  myVals = WorksheetFunction.Transpose(myVals)
  For i = LBound(myVals) To UBound(myVals)
    If myVals(i) Like "19##" Then myVals(i) = CLng("20" & Right$(myVals(i), 2))
  Next i
  
  ActiveSheet.Range("I1").Resize(UBound(myVals), 1).Value2 = WorksheetFunction.Transpose(myVals)
End Sub
 
Upvote 0
Hello,
Given the provided informations, the sub below should give the expected results.
VBA Code:
Sub Replace19xxTo20xx()
  Dim myVals As Variant, i As Long
  With ActiveSheet.Range("I1")
    myVals = Range(.Cells, .End(xlDown)).Value2
  End With
  myVals = WorksheetFunction.Transpose(myVals)
  For i = LBound(myVals) To UBound(myVals)
    If myVals(i) Like "19##" Then myVals(i) = CLng("20" & Right$(myVals(i), 2))
  Next i
 
  ActiveSheet.Range("I1").Resize(UBound(myVals), 1).Value2 = WorksheetFunction.Transpose(myVals)
End Sub

doesn't seem to work ..

 
Upvote 0
Hello,

Well it works fine on Excel, if you are using google sheets that's a different type of macro you need to use, called AppScript. Also you're not on the right forum section if i'm not mistaken.
 
Upvote 0
Hello,

Well it works fine on Excel, if you are using google sheets that's a different type of macro you need to use, called AppScript. Also you're not on the right forum section if i'm not mistaken.
i tried in EXCel, but ill try again..ty
 
Upvote 0
Hello,
Given the provided informations, the sub below should give the expected results.
VBA Code:
Sub Replace19xxTo20xx()
  Dim myVals As Variant, i As Long
  With ActiveSheet.Range("I1")
    myVals = Range(.Cells, .End(xlDown)).Value2
  End With
  myVals = WorksheetFunction.Transpose(myVals)
  For i = LBound(myVals) To UBound(myVals)
    If myVals(i) Like "19##" Then myVals(i) = CLng("20" & Right$(myVals(i), 2))
  Next i
 
  ActiveSheet.Range("I1").Resize(UBound(myVals), 1).Value2 = WorksheetFunction.Transpose(myVals)
End Sub

yah it doesnt work..i think the reason is that....the date in original form is mm/dd/yy

thats why excel asks if you want 19 or 20 as the year

ill upload sample data shortly
 
Upvote 0
yah it doesnt work..i think the reason is that....the date in original form is mm/dd/yy
Can you enter the formula
Excel Formula:
=ISNUMBER(A2)
in a blank cell and change the A2 to one of the cells containing the date and let us know whether you get TRUE or FALSE please?

Also can you post how your dates are displayed please? and what you see in the formula bar when you click one of the cells with a date in it?
 
Upvote 0
Can you enter the formula
Excel Formula:
=ISNUMBER(A2
) in a blank cell and change the A2 to one of the cells containing the date and let us know whether you get TRUE or FALSE please?

Also can you post how your dates are displayed please? and what you see in the formula bar when you click one of the cells with a date in it?

 
Upvote 0
Please enter the formula as requested and post the result and answer the formula bar question
 
Upvote 0
Book1
AB
11/1/19251/1/2025
Sheet1
Cell Formulas
RangeFormula
B1B1=Add100Years(A1)

VBA Code:
Option Explicit
Public Function Add100Years(ByRef rng As Range) As Date
Dim sDate As Date
sDate = rng.Value
Add100Years = DateAdd("yyyy", 100, sDate)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,499
Members
453,047
Latest member
charlie_odd

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