Converting a date to a different date

Belinda

Board Regular
Joined
Apr 5, 2004
Messages
61
I have a database with several hundreds of records with dates that are written incorrectly.
For example, 1/2/1997 is ready by Excel as January 2nd, 1997.
In fact the date is supposed to be February 1st, 1997.
How can I use VBA to convert the date. Thank you.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Belinda
Have you tried simply changing the custom format of the cells to date: dd/mm/yyyy

Regards
Michael M
 
Upvote 0
THe code on this link may work for you:

http://www.ozgrid.com/forum/showthread.php?t=63888

Otherwise you may have to convert the cells to text and then use the text dates with mid, left and right functions to fill in the correct non-US date. I assume you don't need to just change the formatting.

If you have any problems please post back and I can try to come up with some code to convert dates.
 
Upvote 0
Hi Schierln
I had a look at the link and I see the point you are making.
It's a handy explanation too.

As my tag says.........

Regards
Michael M
 
Upvote 0
Ok I was interested in this, so maybe try this:

Code:
Sub changeDate()
Dim c As Range
Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row).TextToColumns DataType:=xlDelimited, FieldInfo:=Array(1, 2)
For Each c In Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
    c.Value = Format(Evaluate("DATE(" & Right(c.Value, 4) & "," & Mid(c.Value, Application.WorksheetFunction.Find("/", c.Value) + 1, Application.WorksheetFunction.Find("/", c.Value, Application.WorksheetFunction.Find("/", c.Value) + 1) - Application.WorksheetFunction.Find("/", c.Value) - 1) & "," & Left(c.Value, Application.WorksheetFunction.Find("/", c.Value) - 1) & ")"), "m/d/yyyy")
Next c
Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row).TextToColumns DataType:=xlDelimited, FieldInfo:=Array(1, 3)
End Sub
Change the column range to your needs. Hope that helps.
 
Upvote 0
I have come across a small problem. Some of the records have "N/A" instead of dates.
How do I adjust the code, so that it skips the cells that don't have dates. Thanks.
 
Upvote 0
Maybe at the very beginning of the for loop put:

Code:
if not isdate(c) then exit sub
Or something to that effect. Hope that helps.
 
Upvote 0
I have come across a small problem. Some of the records have "N/A" instead of dates.
How do I adjust the code, so that it skips the cells that don't have dates. Thanks.

Perhaps

Code:
Sub changeDate()
Dim c As Range
Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row).TextToColumns DataType:=xlDelimited, FieldInfo:=Array(1, 2)
For Each c In Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
    If Not IsError(c.Value) Then
        c.Value = Format(Evaluate("DATE(" & Right(c.Value, 4) & "," & Mid(c.Value, Application.WorksheetFunction.Find("/", c.Value) + 1, Application.WorksheetFunction.Find("/", c.Value, Application.WorksheetFunction.Find("/", c.Value) + 1) - Application.WorksheetFunction.Find("/", c.Value) - 1) & "," & Left(c.Value, Application.WorksheetFunction.Find("/", c.Value) - 1) & ")"), "m/d/yyyy")
    End If
Next c
Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row).TextToColumns DataType:=xlDelimited, FieldInfo:=Array(1, 3)
End Sub
 
Upvote 0
The code still stop when it comes across a cell that has "N/A" in it.
How about adjusting the code so that it will skip any cell that has "N/A"?
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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