Replacing dates in VBA

Adar123

Board Regular
Joined
Apr 1, 2018
Messages
83
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
I am using a replace loop to change certain words.

VBA Code:
Dim oRange As Range

 For Each oRange In ActiveSheet.Range("A1:A10000")
    oRange.Replace What:="AssetName", Replacement:="Commodity", MatchCase:=False
 Next

The challenge I have is with dates. The dates are reported as follows: 20/08/2020. It could be any day of the month but I need to replace it with AUG20. For September dates that would be SEP20.

Thank you.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
If the cells are formatted as dates and only contain dates then it is simple

VBA Code:
Sub ReplaceDates()
    Dim oRange As Range
    Application.ScreenUpdating = False
    For Each oRange In ActiveSheet.Range("A1:A10000")
        With oRange.Offset(, 1)
            .Value = oRange
            .NumberFormat = "mmmyy"
        End With
    Next
End Sub

DatesReformatted.jpg


If that is not what you require, it would help if you posted typical sample data preferably using this tool
 
Upvote 0
Sorry, a follow up question: I was a bit too excited when saw the result. This appears to change the format of the dates presentation but what it needs to change date into a general text such as "AUG20". The cell should not contain anything else other than AUG20. See B2 and other cells in B column to understand the difference in the result sought. Thanks again.

TestMacroBook.xlsm
ABC
1CommodityPillarExpiry
2CBOTCornAUG2014/9/2020
3CBOTCornSep2014/9/2020
4CBOTCornOct2014/12/2020
5CBOTCornNov2014/12/2020
6CBOTCornDec2014/12/2020
NewData
 
Upvote 0
In that case, try this
Rich (BB code):
Sub ReplaceDates()
    Dim oRange As Range
    Application.ScreenUpdating = False
    With ActiveSheet.Range("B1:B10000")
            .NumberFormat = "@"
            For Each oRange In .Cells
                oRange.Value = Format(oRange.Value, "mmmyy")
            Next
    End With
End Sub

or if preferred
oRange.Value = uCase(Format(oRange.Value, "mmmyy"))
 
Upvote 0
Thank you! I think this should do the job. Will be running a few tests with this data over coming days.
 
Upvote 0
I've got an interesting problem with this code, which I have not noticed at first. It is changing all dates to MMMYY but for YY it changes all to "20" e.g. 01-July-2021 becomes JUL20. Could this be to do with all years being 2021,2022,etc and the code picking the two first digits?
 
Upvote 0
Below ... dates are in column A ... desired output is in column B

Book1
AB
104/08/2020Aug20
204/08/2021Aug21
304/08/2022Aug22
404/08/2023Aug23
503/08/2024Aug24
603/08/2025Aug25
703/08/2026Aug26
803/08/2027Aug27
Sheet6

VBA Code:
Sub ReplaceDates()
    Dim oRange As Range
    Application.ScreenUpdating = False
    With ActiveSheet.Range("A1:A8")
            .Offset(, 1).NumberFormat = "@"
            For Each oRange In .Cells
                oRange.Offset(, 1).Value = Format(oRange.Value, "mmmyy")
            Next
    End With
End Sub
 
Upvote 0
I show three columns. A is the data in its original form. Column B is what happened to the original data and column C shows the final output.

VBA Code:
Dim bRange As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row

       Application.ScreenUpdating = False
    With ActiveSheet.Range("B2:B" & lastRow)
            .Offset(, 1).NumberFormat = "@"
            For Each bRange In .Cells
                bRange.Offset(, 1).Value = Format(bRange.Value, "mmmyy")
            Next
    
    End With



Ags_20200804.xls
ABC
1OriginalDateExpiry
2Sep-20Sep-20Sep20
3Oct-20Oct-20Oct20
4Nov-20Nov-20Nov20
5Dec-20Dec-20Dec20
6Jan-21Jan-21Jan20
7Feb-21Feb-21Feb20
8Mar-21Mar-21Mar20
9Apr-21Apr-21Apr20
10May-21May-21May20
11Jun-21Jun-21Jun20
12Jul-21Jul-21Jul20
13Aug-21Aug-21Aug20
14Sep-21Sep-21Sep20
15Oct-21Oct-21Oct20
16Nov-21Nov-21Nov20
17Dec-21Dec-21Dec20
18Jan-22Jan-22Jan20
19Feb-22Feb-22Feb20
20Mar-22Mar-22Mar20
21Apr-22Apr-22Apr20
NewData
 
Upvote 0
Sorry - I do not understand what you want
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,015
Members
449,203
Latest member
tungnmqn90

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