Date changes to US format when using VBA

hocohen

New Member
Joined
Feb 21, 2023
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
The issue is:

1. I export SAP report to Excel.
2. One of the fields is a Date in Text format "dd.mm.yyyy"
3. In order to convert it to a Date format - I replace the "." into "/", and format the field as date "dd.mm.yyyy".
4. If I do the replacement in Excel, the result is correct "dd/mm/yyyy", but If I do it in VBA - the day and month alternate and the result is "mm/dd/yyyy".

I understand it happens because the VBA format is always US.
The question is if there is a solution for that.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
A simple workaround would be extracting the date parts (day, month, year) from the text String and creating a VBA date from them.

Assuming, you have a date formatted as "dd.mm.yyyy" in cell A1, the following VBA code would create a date object:

VBA Code:
Sub txt2date()
   Dim X As Date
   Dim d As String
   Dim m As String
   Dim y As String
  
   d = left(Range("A1").Value, 2)
   m = mid(Range("A1").Value, 4, 2)
   y = Right(Range("A1").Value, 2)
   X = DateSerial(y, m, d)
  
   MsgBox X
End Sub

The Message Box is just for showing the resulting date.
Range() should be used with the correct Workbook and Worksheet like ThisWorksheet.Sheets("Sheet1").Range()
 
Upvote 0
but If I do it in VBA - the day and month alternate and the result is "mm/dd/yyyy".

Usually, using Cdate function is enough to solve this kind of problem.
If not, try using DateSerial:
VBA Code:
Sub try()
Dim tx As String
Dim dt As Date
    tx = "02.03.2023"
    a = Split(tx, ".")
    dt = DateSerial(a(2), a(1), a(0))
    Debug.Print dt   'returns 02/03/2023
    Debug.Print Month(dt) 'returns 3
End Sub
Note: My system uses DMY format.
 
Upvote 0
Using Cdate:
VBA Code:
Sub try2()
Dim tx As String
Dim dt As Date
    tx = "02.03.2023"
    dt = CDate(Replace(tx, ".", "/"))
    Debug.Print dt   'returns 02/03/2023
    Debug.Print Month(dt) 'returns 3
End Sub
Note: My system uses DMY format.
 
Upvote 0
A simple workaround would be extracting the date parts (day, month, year) from the text String and creating a VBA date from them.

Assuming, you have a date formatted as "dd.mm.yyyy" in cell A1, the following VBA code would create a date object:

VBA Code:
Sub txt2date()
   Dim X As Date
   Dim d As String
   Dim m As String
   Dim y As String
 
   d = left(Range("A1").Value, 2)
   m = mid(Range("A1").Value, 4, 2)
   y = Right(Range("A1").Value, 2)
   X = DateSerial(y, m, d)
 
   MsgBox X
End Sub

The Message Box is just for showing the resulting date.
Range() should be used with the correct Workbook and Worksheet like ThisWorksheet.Sheets("Sheet1").Range()

How to use it for a range (column with varies number of rows)?
 
Upvote 0
Please stop duplicate the same response many times!
As per forum rules, posts of a duplicate nature will be deleted.
 
Upvote 0

Akuini

How to use Cdate for a range (column with varies number of rows)?
Try:
VBA Code:
Sub try4()
Dim tx As String
Dim dt As Date
Dim c As Range
Set c = Range("A2:A10")  'adjust the range
    va = c
    For i = 1 To UBound(va, 1)
        va(i, 1) = CDate(Replace(va(i, 1), ".", "/"))
    Next
    c.NumberFormat = "dd-mm-yyyy"
    c = va
End Sub
 
Upvote 0
Macro stopped at:
For i = 1 To UBound(va, 1)
Type mismatch
 
Upvote 0
I used Akuini's method inside a range loop:
VBA Code:
Sub MyDateMacro()

    Dim lr As Long
    Dim cell As Range
   
    Application.ScreenUpdating = False
   
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Loop through all cells in column A starting on row 2
    For Each cell In Range("A2:A" & lr)
        cell = CDate(Replace(cell, ".", "/"))
        cell.NumberFormat = "dd-mm-yyyy"
    Next cell
   
    Application.ScreenUpdating = True
   
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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