Dates randomly chaning format (VBA)

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hi guys,

I have a spreadsheet which is populated by an SAP report. I made a macro to make changes to the sheet and make it look pretty etc.

All works well, apart from the dates seem to switch to US format randomly:

07.02.2011 changes to 02/07/2011
01.05.2011 changes to 05/01/2011
15.08.2011 stays the same (but with /'s)

Here's the problematic bit of my code:

Code:
        With Range("F:F")
            .Replace What:=".", Replacement:="/" 
            .NumberFormat = "dd/mm/yyyy"
        End With
Why doesn't that do what it's supposed to?! Removing .NumberFormat makes no difference aswell.

The original dates are stored as Text when they are pulled from SAP (Not sure if this would change anything).
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Bump!

I recorded myself doing Ctrl+H - Replace . with / and copied the result in place of my current bit of code. The manual replace worked fine (didn't change the dates to US) but the macro does...

How on earth can it work for me manually, but not in VBA?

Confusion ++
 
Upvote 0
It's the joy of VBA being US-centric. You need something like:
Code:
   Dim varDates
   Dim lngIndex As Long
   With Range("F1:F" & Cells(Rows.Count, "F").End(xlUp).Row)
      varDates = .Value
      For lngIndex = LBound(varDates) To UBound(varDates)
         varDates(lngIndex, 1) = CDate(Replace(varDates(lngIndex, 1), ".", "/"))
      Next lngIndex
      .Value = varDates
      .NumberFormat = "dd/mm/yyyy"
   End With

note there is no checking for blank cells/text/non-dates etc.
 
Upvote 0
Like so:
Code:
   Dim varDates
   Dim lngIndex As Long
   With Range("F1:F" & Cells(Rows.Count, "F").End(xlUp).Row)
      varDates = .Value
      For lngIndex = LBound(varDates) To UBound(varDates)
         If Len(varDates(lngIndex, 1)) > 0 Then varDates(lngIndex, 1) = CDate(Replace(varDates(lngIndex, 1), ".", "/"))
      Next lngIndex
      .Value = varDates
      .NumberFormat = "dd/mm/yyyy"
   End With
 
Upvote 0
Thanks Rory. It still put them as US dates, so I got rid of CDate and it's now working fine!

Appreciate your help.
 
Upvote 0
That makes no sense at all to me. If you don't use CDate you should get the same behaviour you had originally.
 
Upvote 0
Yeah that's what I thought... I have no idea why it's doing what it's doing, but it seems to be working.

When I ran your code (with CDate) all of my dates were US (rather than random ones), also they were in this format: 4/18/2011 rather than 04/18/2011. Removing CDate seems to work (dates are 18/04/2011).

Could it have anything to do with the original data - It's stored as text.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

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