Dates converting to American format when using CDATE?

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I have an issue with the code below where I am attempting to amend incorrectly formatted dates so any instance of "." should change to "/".

The first piece of code seems to convert everything incorrectly:

Code:
Sub DateConv()


    Dim LrDst As Long
    Dim DstSht As Worksheet
    Dim StrDate As String
    Dim c As Range
    
    Set DstSht = ActiveSheet
    LrDst = DstSht.Cells(Rows.Count, "A").End(xlUp).Row


    For Each c In DstSht.Range("A1:A" & LrDst)
        If IsDate(c.Value) And c.Value <> "" Then
            StrDate = Left(c, 2) & "/" & Mid(c, 4, 2) & "/" & Mid(c, 7, 100)
            c.Value = Format(CDate(StrDate), "dd/mm/yyyy")
        End If
    Next c
    
End Sub


bf1d9y.png
[/IMG]



The second piece of code works slightly better but still has errors.

Code:
Sub DateConv()


    Dim LrDst As Long
    Dim DstSht As Worksheet
    Dim StrDate As String
    Dim c As Range
    
    Set DstSht = ActiveSheet
    LrDst = DstSht.Cells(Rows.Count, "A").End(xlUp).Row


    For Each c In DstSht.Range("A1:A" & LrDst)
        If IsDate(c.Value) And c.Value <> "" Then
            StrDate = Left(c, 2) & "/" & Mid(c, 4, 2) & "/" & Mid(c, 7, 100)
            c = StrDate
            c.Value = Format(CDate(c), "dd/mm/yyyy")
        End If
    Next c
    
End Sub

2432eeo.jpg
[/IMG]

Any suggestions please or a better way of dealing with this issue?

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try
Code:
Sub DateConv()
    Dim LrDst As Long
    Dim DstSht As Worksheet
    
    Set DstSht = ActiveSheet
    LrDst = DstSht.Cells(Rows.Count, "A").End(xlUp).Row

    DstSht.Range("A1:A" & LrDst).Replace ".", "/", xlPart, , , , False, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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