VBA Codes to Format Dates Using Text-To-Column Feature of Excel

 Sometimes we come across some dates which are formatted in US date format or in text format. When we try to find the Day, Month or Year out of it, we get an error.

Usually, we use the excel Text-To-Column feature to format the date column with default options (Delimited, not Fixed width). This works for dates less than the 13th day of the month but not for dates greater than the 12th day of the month.

Sometimes this becomes very painful especially if you are working on something which has to be delivered in a short span of time.

In this situation, we need to dig deep into the Text-To-Column feature. In step 3, under Column data format, in Date, we should select either DMY or MDY to format the dates in the correct format.

We may do this using VBA also and the codes are as follows:

Let's say, our data is present in Column1 in Sheet1.

1) For Normal Format (Just Next-Next, default options)

Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _

        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

        :=Array(1, 1), TrailingMinusNumbers:=True

2) When we select DMY under Column data format (Step 3), in Date

Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _

        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

        :=Array(1, 4), TrailingMinusNumbers:=True

3) When we select MDY under Column data format (Step 3)in Date

Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _

        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

        :=Array(1, 3), TrailingMinusNumbers:=True

Here as we can see in the above codes only FieldInfo needs to be changed to change the Date Format.

Comments

Popular posts from this blog

Power Automate - Automatically fetch data from Power BI in to Excel and Send the copy of the Excel file via Email

Function to Paste Excel Range on Outlook Mail Body (In text not image) Function Name - rngHTML()

Separate Text (Characters) & Numbers from Alpha Numeric String Using Formula and Macro (VBA)