How to change date format in VBA
Try this code assuming General format for Column A:
Sub ConvertDate()
Dim x As Variant, LRow As Long
Dim Drng As Range, c As Range
LRow = Cells(Rows.Count, "A").End(xlUp).Row
Set Drng = Range("A1:A" & LRow)
For Each c In Drng
x = c.Value
c.Value = Format(Replace(x, ".", "/"), "yyyymmdd")
Next
End Sub
Mike F
"Frank" wrote in message
...
I want to change the date format in VBA from "13.07.2006" to "20060713".
If I try to record a macro applying the Format, Format cells... function,
nothing happends.
The excel workbook has date values in column A with the wrong format
"13.07.2006". The correct format is "20060713".
I try to open the workbook and create a new column B with correct date
format.
Then I try to overwrite column A with a pasteSpecial command.
When I run the macro, I return runtime error 1004.
Any suggestions to make this work?
Regards
Frank Krogh
Sub ChangeDateFormat()
strFilename = Application.GetOpenFilename("Report (*.xls),*.xls")
Application.ScreenUpdating = False
If strFilename < "False" Then
Workbooks.Open strFilename
Columns("B:B").Select
Application.Workbooks(Workbooks.Count).Activate
With Selection
.Formula = "=CONCATENATE(LEFT(A,7,4),(LEFT(A,1,2),(LEFT(A,4,2 ))"
End With
Columns("B:B").Select
Selection.Copy
Columns("A:A").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End If
End Sub
|