I have a range of dates which I need to convert to 'MM/DD/YYYY format (but as text) every month.
I used to manually convert these by using this formula =TEXT(Cell Ref.,"MM/DD/YYYY")
. See picture above. I have recently started using below VBA code to save my time (there are around 18 columns with 200K rows worth of data every month).
Sub MM_DD_YYYY()
Application.ScreenUpdating = False
Dim rng As Range
Selection.NumberFormat = "0"
For Each rng In Selection
rng.Value = "+text(" & rng.Value & ",""MM/DD/YYYY"")"
Next rng
Selection.TextToColumns DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
This code works fine if I select one column but fails if I select multiple columns because it has text to column element (which obviously only works for one column at a time). Is it possible to run the code one column at a time after selecting entire range without breaking it?
By the way, I have tried below alternatives of text to column:
- Simulating F2+Enter. This works but takes a lot of time.
For Each rng In Selection
SendKeys "{F2}", True
SendKeys "{ENTER}", True
Next
- Doesn't work for some reason.
Selection.Value = Selection.FormulaR1C1
- Doesn't work for some reason.
For Each rng In Selection
Selection.Value = Selection.Value
Next rng
I would really appreciate your help or suggestion here. Thanks.