VBA to change US date format to UK date format

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi everyone

I have a report which have dates in American format YYYY-MM-DD, I need to change the date columns from D to M into UK format DD-MM-YYYY and the end row is various. Some fields may be only text instead of dates.

Please could anyone help how to create this in VBA? Many thanks.
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See if this works for you.

Code:
Sub formatdate()
Dim lr As Long
lr = Cells(Rows.Count, "D").End(xlUp).Row [COLOR=#008000]'change "D" to column that will always have value in it[/COLOR]
Range("D2:M" & lr).NumberFormat = "DD-MM-YYYY"

End Sub
 
Upvote 0
Are they excel dates are text?

you can use isnumber to test if it is a number or not, dates in excel are numbers formatted to look like a date. Where D2 has the date
Code:
=ISNUMBER(D2)
 
Upvote 0
You are right, they are not numbers as the result is FALSE. So how to change text from 2018-09-17 to 17-09-2018 please?
 
Upvote 0
See if this works for you

Code:
Sub formatdate()
Dim lr As Long
lr = Sheets("Sheet6").Cells(Rows.Count, "D").End(xlUp).Row 'change "D" to column that will always have value in it
Sheets("Sheet6").Range("D2:M" & lr).NumberFormat = "DD-MM-YYYY"
For Each cell In Sheets("Sheet6").Range("D2:M" & lr)
    Range(cell.Address) = DateValue(cell)
Next cell
End Sub
 
Upvote 0
I have updated the code to "Sheet1" however, there is run time error 9 out of range. Therefore I changed it to the name of the worksheet "Data". It seems worked. However, there is run time error 13 for

Range(cell.Address) = DateValue(cell)

Is it possible to use the generic Sheet1 instead of the name of the worksheet "Data"?
 
Last edited:
Upvote 0
The code should point to the sheet you are working on so if the sheet is named data then it should point to data. If there are any blank cells in the range you can not get the datevalue of that cell see if this fixes the problem.

Code:
Sub formatdate2()
Dim lr As Long
lr = Sheets("Data").Cells(Rows.Count, "D").End(xlUp).Row 'change "D" to column that will always have value in it
Sheets("Data").Range("D2:M" & lr).NumberFormat = "DD-MM-YYYY"


For Each cell In Sheets("Data").Range("D2:M" & lr)
    If cell <> "" Then Range(cell.Address) = DateValue(cell)
Next cell


End Sub
 
Upvote 0
Working on a column (rather than cell) at a time, this should work on the active sheet and assumes no blank columns from D:M.
Test in a copy of your workbook.
Code:
Sub Fix_Dates()
  Dim c As Long
  
  For c = 4 To 13   '<- Cols D:M
    Columns(c).TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 5)
    Columns(c).NumberFormat = "dd-mm-yyyy"
  Next c
End Sub
 
Last edited:
Upvote 0
Please ignore as won't make a difference with OP's format
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

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