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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,595
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Hi Belinda
Have you tried simply changing the custom format of the cells to date: dd/mm/yyyy

Regards
Michael M
 
Upvote 0

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
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

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,595
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
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

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
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

Belinda

Board Regular
Joined
Apr 5, 2004
Messages
61
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

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,941
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

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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

Belinda

Board Regular
Joined
Apr 5, 2004
Messages
61
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,191,032
Messages
5,984,248
Members
439,879
Latest member
KingGoulash

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
Top