1

I have a range of dates which I need to convert to 'MM/DD/YYYY format (but as text) every month.

Range

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:

  1. Simulating F2+Enter. This works but takes a lot of time.
For Each rng In Selection
    SendKeys "{F2}", True
    SendKeys "{ENTER}", True
Next
  1. Doesn't work for some reason.
Selection.Value = Selection.FormulaR1C1
  1. 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.

ram singh
  • 41
  • 8
  • 2
    Why you cannot just copy all data into another column and change the date format will do??? Custom format`mm/dd/yyyy` lol? – Kin Siang May 27 '21 at 12:13
  • 2
    `Selection.NumberFormat = "MM/DD/YYYY"` – Siddharth Rout May 27 '21 at 12:15
  • It is possible, but why to iterate and use TextToColumns? – FaneDuru May 27 '21 at 12:16
  • 2
    Quick question.... `I have a range of dates` Are these actual dates or dates stored as Text? – Siddharth Rout May 27 '21 at 12:17
  • Sorry if I was not clear. I can't just format cells because I have to load data to a different system which only accepts this format. The output has a apostrophe at the beginning i.e. it's a text. That is why I was using text formula. `Selection.NumberFormat = "MM/DD/YYYY"` also doesn't work. range of dates are actual dates but output should be a text. – ram singh May 27 '21 at 12:22
  • @SiddharthRout Just curious, is it possible to make it to work for more than one range. Example, I have dates in Col A and Col C (Col B has some other data). Current code doesn't work because if I select only Col A and Col C, they are now 2 ranges. Any thoughts? – ram singh May 27 '21 at 13:43
  • 1
    Yup. Updated my post. You may have to refresh the page to see it. – Siddharth Rout May 27 '21 at 14:01

1 Answers1

2

The output has a apostrophe at the beginning i.e. it's a text. That is why I was using text formula. Selection.NumberFormat = "MM/DD/YYYY" also doesn't work. range of dates are actual dates but output should be a text. – ram singh 12 secs ago

Try this. For explanation see Convert an entire range to uppercase without looping through all the cells. The below code uses INDEX() and TEXT().

Option Explicit

Sub Sample()
    Dim rng As Range
    Dim sAddr As String

    Set rng = Range("A1:C5") '<~~ Change this to relevant range
    sAddr = rng.Address

    rng = Evaluate("index(""'"" & Text(" & sAddr & ",""MM/DD/YYYY""),)")
End Sub

BEFORE:

enter image description here

AFTER:

enter image description here

EDIT

@SiddharthRout Just curious, is it possible to make it to work for more than one range. Example, I have dates in Col A and Col C (Col B has some other data). Current code doesn't work because if I select only Col A and Col C, they are now 2 ranges. Any thoughts? – ram singh 15 mins ago

Is this what you want?

Option Explicit

Sub Sample()
    Dim rng As Range
    Dim ar As Range
    Dim sAddr As String

    Set rng = Range("A1:A5,C1:C5") '<~~ Sample range
    
    For Each ar In rng.Areas
        sAddr = ar.Address

        ar = Evaluate("index(""'"" & Text(" & sAddr & ",""MM/DD/YYYY""),)")
    Next ar
End Sub

BEFORE:

enter image description here

AFTER:

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    @ram singh: I would like to remind you that we here, when somebody answer our question, we tick the code left side check box, in order to make it **accepted answer**. In this way, somebody else searching for a similar issue, will know that the code works... – FaneDuru May 27 '21 at 13:09