Macro for Text to Columns

gammaves

New Member
Joined
Mar 20, 2017
Messages
28
Whenever Google Analytics exports a date, it shows up as


Date
20220117
20220118
20220101
20220102
20211227

The best way to make it into a format usable by excel is to use the Text to columns button.
I chose 'Delimited', but uncheck all of the different delimiters. Then on the Column Data Format page of it, I choose date.

I wanted to create a macro that looks for a column with the header "Date" and then applies this formatting to it.

Thank you for helping me out!
 

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).
Try this (assuming row 1 is your header row):
VBA Code:
Sub MyDateMacro()

'   Find column with the word "Date" in row 1
    Rows("1:1").Find(What:="Date", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

'   Split column
    ActiveCell.EntireColumn.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True

End Sub
 
Upvote 0
Try this (assuming row 1 is your header row):
VBA Code:
Sub MyDateMacro()

'   Find column with the word "Date" in row 1
    Rows("1:1").Find(What:="Date", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

'   Split column
    ActiveCell.EntireColumn.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True

End Sub

Thanks Joe, It looks like it worked, but it put the proper dates in the next column over, overwriting anything that was there. Is there a way for the destination to just overwrite whatever is in the Date column?
 
Upvote 0
Sorry, forgot to remove the hard-reference to cell D1.

Try this:
VBA Code:
Sub MyDateMacro()

'   Find column with the word "Date" in row 1
    Rows("1:1").Find(What:="Date", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

'   Split column
    ActiveCell.EntireColumn.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True

End Sub
 
Upvote 0
Here is a more robust version that will tell you it cannot find the word "Date" instead of returning an error, if it is possible the word may not appear on row 1:
VBA Code:
Sub MyDateMacro()

    On Error GoTo err_chk

'   Find column with the word "Date" in row 1
    Rows("1:1").Find(What:="Date", After:=Range("A1"), LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

'   Split column
    ActiveCell.EntireColumn.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 5), TrailingMinusNumbers:=True
        
    Exit Sub
    
err_chk:
    If Err.Number = 91 Then
        MsgBox "Cannot find the word 'Date' in row 1", vbOKOnly
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,560
Messages
6,125,523
Members
449,236
Latest member
Afua

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