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.
 
Macro stopped at:
For i = 1 To UBound(va, 1)
Type mismatch

untested but see if this update to @Akuini suggested array code resolves the issue

VBA Code:
Sub tryAgain()
    Dim arr                       As Variant
    Dim rng                      As Range
  
    '------------------------------------------------------------------------------------------------------------------------
    '                                                       SETTINGS
    '-----------------------------------------------------------------------------------------------------------------------
    Const DateColumn    As Long = 1                   '<< 'change as required
    Const DateFormat     As String = "dd-mm-yyyy"     '<<
    '-----------------------------------------------------------------------------------------------------------------------
  
    'size the date column range
    Set rng = Cells(2, DateColumn).Resize(Cells(Rows.Count, DateColumn).End(xlUp).Row - 1)
  
    'intialize array
    arr = rng.Value
  
    For i = 1 To UBound(arr, 1)
       If arr(i, 1) Like "##.##.####" Then
            arr(i, 1) = CDate(Replace(arr(i, 1), ".", "/"))
       End If
    Next
  
    'format range
    rng.NumberFormat = DateFormat
    'post array to range
    rng = arr
  
End Sub

Dave
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Joe4

Compile error on line:
For Each cell In Range("A2:A" & lr)
"For Each control variable must be Variant or Object"
 
Upvote 0
I originally had a typo in my code which I fixed yesterday.
Maybe you tried it out before the fix.

Try copying the code from that post now, and see if it works.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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