Text to Columns Wizard

excelnoobnoob

New Member
Joined
Jun 27, 2017
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a data connection to a csv in my excel workbook.
One of the columns contains dates in mm/dd/yy format.
Excel won't accept 'format as date', so I searched online and found a solution (use the Convert Text to Columns Wizard).
This works! The column is now formatted correctly as date.
The only issue is, whenever the data source refreshes, the column reverts to the original, general format.

Does anybody know what can be done to resolve such an issue?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about using a sheet event, for example, assuming that the data source has been updated, it means A1 has changed. Then running the code executes.

Please paste below code on the sheet module.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then
        Exit Sub
    Else
        'call or paste here your code
    End If
End Sub
 
Upvote 0
How about using a sheet event, for example, assuming that the data source has been updated, it means A1 has changed. Then running the code executes.

Please paste below code on the sheet module.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then
        Exit Sub
    Else
        'call or paste here your code
    End If
End Sub
Thanks Takae - I *think* cell A1 doesn't change when the source has been updated. (when refreshing, rows are just added to the bottom of the table).
I could be wrong tho.

I've tried this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then
        Exit Sub
    Else
    Columns("C:C").Select
    Selection.TextToColumns Destination:=Range("TLog[[#Headers],[Column1]]"), _
        DataType:=xlFixedWidth, FieldInfo:=Array(0, 3), TrailingMinusNumbers:=True
    End If
End Sub

But on data refresh it isn't being triggered.
I looked at some alternative methods (detecting new row on table - which is tricky/affects other sheets from most examples I've seen)
Did you have any ideas?
Thanks so much for your help
 
Upvote 0
EDIT: I also tried changing:
VBA Code:
If Intersect(Target, Range("A1")) Is Nothing Then

to:
VBA Code:
If Intersect(Target, Range("C2")) Is Nothing Then
Since this is the column where the formatting is reverting. I would have assumed that when the data refreshes, this cell changes (the value doesn't change, but the format does).

Still doesn't execute tho :(

This macro does work:
VBA Code:
    Columns("C:C").Select
    Selection.TextToColumns Destination:=Range("TLog[[#Headers],[Column1]]"), _
        DataType:=xlFixedWidth, FieldInfo:=Array(0, 3), TrailingMinusNumbers:=True
It just isn't being triggered for whatever reason.
 
Upvote 0
If the data just add rows, how about this one.
It will be executed when new data is added somewhere from column A. If the leftmost column is column C, set it to C.(It doesn't have to be the leftmost column though.)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A:A")) Is Nothing Then' or C:C? This line is trigger.
        Exit Sub
    Else
        Application.EnableEvents = False
        'call or paste here your code
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
I can feel im getting closer! Maybe...
Now when I hit refresh, I get an unexpected function or variable message.

This is the code for reference:

1653395891922.png
 
Upvote 0
Are you going to run another macro of another book? This code run after you update the data.
"Application.Run...." this line should be a code to change the format.
I thought you already had the code to change the format. If you don't have one, please let me know which column you would like to change.
And I want to make sure your goal is "mm / dd / yy".
 
Upvote 0
I thought I did have the code but it does not work ( I generated by recording a macro)
The column is C, and I’m trying to set it to mm/did/yyyy like choosing MDY in text to columns does.
 
Upvote 0
Are you going to run another macro of another book? This code run after you update the data.
"Application.Run...." this line should be a code to change the format.
I thought you already had the code to change the format. If you don't have one, please let me know which column you would like to change.
And I want to make sure your goal is "mm / dd / yy".
Sorry forgot to mention the macro I am running is in the same book
 
Upvote 0
I put code for changing format after "Else". So, if you refresh the data as usually, this macro runs automatically and changes format on Col C.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LR As Long, i As Long, x

    If Intersect(Target, Range("C:C")) Is Nothing Then
        Exit Sub
    Else
        Application.EnableEvents = False
        Range("C:C").NumberFormatLocal = "mm/dd/yy;@"
        LR = Cells(Rows.Count, 3).End(xlUp).Row
        x = Range(Range("C2"), Cells(LR, 3))
        For i = 1 To UBound(x)
            x(i, 1) = CDate(Format(x(i, 1), "##/##/####"))
        Next
        Range(Range("C2"), Cells(LR, 3)) = x
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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