I would like to know how to convert date "17/03/10" to "10/03/17".
17 is the year, 03 is the month and 10 is the day. Can I get a VBA code for this to use in Excel?
I would like to know how to convert date "17/03/10" to "10/03/17".
17 is the year, 03 is the month and 10 is the day. Can I get a VBA code for this to use in Excel?
I've assumed because of your use of quotes that these are strings representing dates you want to convert, and not date values.
Therefore, try this:
strOldDate = "17/03/10"
strNewDate = Mid$(strOldDate, 7, 2) & "/" & _
Mid$(strOldDate, 4, 2) & "/" & _
Mid$(strOldDate, 1, 2)
MsgBox strNewDate 'Outputs "10/03/17"
Try the below, I've placed in into a function so you should be able to more easily include it within your code (And even use it in formula)
'// The actual function
Function ReformatDate(WeirdDate As String) As Date
ReformatDate = Right(WeirdDate, 2) _
& Mid(WeirdDate, 3, 4) _
& Left(WeirdDate, 2)
End Function
'// Testing with whatever is selected
Sub TestFixSelection()
For Each Cell In Selection
Cell.Value = ReformatDate(Cell.Value)
Next
End Sub
'// Testing over a fixed range
Sub TestFixFixedRange()
For Each Cell In [A1:A10]
Cell.Value = ReformatDate(Cell.Value)
Next
End Sub
Be aware this corrects the string and turns it into a date, not a string representation of a date. Don't be afraid of Excel Dates, they're your friends.