Issue with American Format when VBA "Find and Replace"

Lars_C

New Member
Joined
Aug 21, 2017
Messages
7
Hey there,

Apologies, this seems very stupid. I am fully aware of the issue with the American date when using VBA find and replace function. I have read about it and took onboard that others suggested, but it still doesn't work for me.

I might be doing something very silly here, but any chance someone could have a quick check of my simple code please.

Thank you,
Lars

Code:
Sub FormatDate()
'
' FormatDate Macro
'
    Columns("A:A").Select
    Selection.NumberFormat = "dd/mm/yyyy;@"
    Selection.Replace What:=".", Replacement:="/", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi,

You know your dates are actually numbers ... which are displayed with a custom format ...

As long as your column A is holding numbers ... for your macro is concerned ...you can test following

Code:
Sub FormatDate()
'
' FormatDate Macro
    Columns("A:A").NumberFormat = "dd mm yyyy"
End Sub

The trick is that you will have to go to your Control Panel ....
Customize Regional Options > Date > Date separator

HTH
 
Upvote 0
Try this...

Code:
[color=darkblue]Sub[/color] FormatDate()
[color=green]'[/color]
[color=green]' FormatDate Macro[/color]
[color=green]'[/color]
    [color=darkblue]With[/color] Columns("A:A")
        .NumberFormat = "dd/mm/yyyy;@"
        .TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
            FieldInfo:=Array(1, 4), TrailingMinusNumbers:=[color=darkblue]True[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thank you both for helping me on this.

James, unfortunately I couldn't get your suggestion to work. It reformats the date fine, but does not replace the ".". I tried linking that with my script, but not successful.

AlphaFrog, your suggestion worked well. The only thing is that if I run the macro twice, it will swap between the American and British format, e.g. 01/08/2017 becomes 08/01/2017 and back around again.
It is not a big deal, but out of interest how would I limit the action just to show the correct format, rather than switch in between?

Thanks again,
Lars
 
Upvote 0
James, unfortunately I couldn't get your suggestion to work. It reformats the date fine, but does not replace the ".". I tried linking that with my script, but not successful.

As already explained, the Date separator is a Windows set-up ... not an Excel feature ...

So, you need to adjust your Regional settings ...
 
Upvote 0
AlphaFrog, your suggestion worked well. The only thing is that if I run the macro twice, it will swap between the American and British format, e.g. 01/08/2017 becomes 08/01/2017 and back around again.
It is not a big deal, but out of interest how would I limit the action just to show the correct format, rather than switch in between?

I can't duplicate the toggle effect. Not sure how to fix it if I could.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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