vba range.replace not setting date format properly

Mushy peas

New Member
Joined
Jun 14, 2023
Messages
33
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have a range (let's call it the A column) that has dates in this format: dd.mm.yyyy so I want to change the format to short dates d/m/yyyy

Instead when I use range.replace, it takes the values as if it is in the mm/dd/yyyy format. How do I replace it.


VBA Code:
Sub format_dates()
    Sheet1.Columns(1).Replace ".", "/"
End Sub
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
VBA Code:
Sub format_dates()
Sheet1.Columns(1).NumberFormat = "d/m/yyyy"
End Sub
 
Upvote 0
dd.mm.yyyy I mean
Yes. I know.
Before running the code:
Book1
A
114.06.2023
215.06.2023
316.06.2023
417.06.2023
518.06.2023
619.06.2023
720.06.2023
821.06.2023
922.06.2023
1023.06.2023
Sheet1


After running the code:
Book1
A
114/6/2023
215/6/2023
316/6/2023
417/6/2023
518/6/2023
619/6/2023
720/6/2023
821/6/2023
922/6/2023
1023/6/2023
Sheet1
 
Upvote 0
Possibly what you have are text values that look like dates and not actually dates? In a cell type =A1+1 (assuming you have a 'date' in A1). If it comes back as #VALUE! then you don't have actual dates in column A but rather text values that look like dates.
 
Upvote 0
Possibly what you have are text values that look like dates and not actually dates? In a cell type =A1+1 (assuming you have a 'date' in A1). If it comes back as #VALUE! then you don't have actual dates in column A but rather text values that look like dates.
Hmm somehow the values in my sheet just remains in the form dd.mm.yyyy
 
Upvote 0
Are the 'dates' left-aligned in their cells, or do they have a ' at the left of the cell they're in? Did you try what I suggested in post #6?
 
Upvote 0
Ahhh I see what you mean. It is currently formatted as text so your method sadly does not work.
 
Upvote 0
I can't get Evaluate to work properly on a single line of code, so try this (until a better solution comes along). Assumes we're looking at Sheet1 and the data starts from row 2 - adjust to suit.

VBA Code:
Sub format_dates()
    Dim LRow As Long, LCol As Long
    LRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
    LCol = Sheet1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    With Sheet1.Range(Sheet1.Cells(2, LCol), Sheet1.Cells(LRow, LCol))
        .FormulaR1C1 = "=TEXT(DATEVALUE(SUBSTITUTE(RC1,""."",""/"")),""d/m/yyyy"")"
        .Value = .Value
        .Copy Sheet1.Range("A2")
    End With
    Sheet1.Columns(LCol).ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

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