Text to Columns ? VBA

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,125
Office Version
  1. 365
Platform
  1. Windows
Hi I need to add more columns on this but it breaks. Doing something wrong or maybe I cant do it? and woul dhave to run this multiple times for different columns?

("D:D.U:U,T:T") -- I tried this but no luck

Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Sub Correction1() 'Text to Columns'[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]With Range("D:D")[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    .TextToColumns[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    .NumberFormat ="General"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    ActiveSheet.Name ="Payroll Week Ending"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End With[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Text to Columns can only be applied to one column at a time. However, you can store all the columns you want to apply it to in an array and loop through that array, like this:
Code:
Sub MyTtoC()

    Dim cols As Variant
    Dim i As Long
    Dim c As String
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   List columns you want to apply Text to Columns to
    cols = Array("D", "T", "U")
    
'   Loop through array
    For i = LBound(cols) To UBound(cols)
'       Get column letter
        c = cols(i)
'       Build range
        Set rng = Range(c & ":" & c)
'       Perform Text to Columns
        With rng
            .TextToColumns
            .NumberFormat = "General"
        End With
    Next i
    
'   Name sheet
    ActiveSheet.Name = "Payroll Week Ending"
    
    Application.ScreenUpdating = True
    
End Sub
So, to add/remove columns, simply update this line here:
Code:
    cols = Array("D", "T", "U")
 
Upvote 0
k thanks I would think that it can be added to the range I did and be simple like that, but guess not been trying all morning thanks for the help like always
 
Upvote 0
You are welcome.

Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]With Range[/COLOR][/SIZE][/FONT]("D:D,U:U,T:T")
would try to do them all at the same time, which will blow up Text to Columns.

You need to iterate your way through the columns. Using arrays is a good way to iterate through any list in VBA.
 
Last edited:
Upvote 0
HI i came back to this because i ran into an issue. For some reason column AD i received. There is data in this already do you wish to etccc.

Well by troubleshooting i found out why. When i click text to columns the dot is in Fixed width. When all other are in Delimited which work fine. Anything i can add into the doe to fixed this so its doesn't break checking AD.
 
Upvote 0
Try changing this line:
Code:
.TextToColumns
to this:
Code:
.TextToColumns DataType:=xlDelimited
 
Upvote 0
it works thanks a lot. Sometimes i think why didn't i think of that. Maybe recording what i needed and take that piece lol. Thanks for the reply
 
Upvote 0
This is fantastic, thank you Joe4!
Text to Columns can only be applied to one column at a time. However, you can store all the columns you want to apply it to in an array and loop through that array, like this:
Code:
Sub MyTtoC()

    Dim cols As Variant
    Dim i As Long
    Dim c As String
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   List columns you want to apply Text to Columns to
    cols = Array("D", "T", "U")
    
'   Loop through array
    For i = LBound(cols) To UBound(cols)
'       Get column letter
        c = cols(i)
'       Build range
        Set rng = Range(c & ":" & c)
'       Perform Text to Columns
        With rng
            .TextToColumns
            .NumberFormat = "General"
        End With
    Next i
    
'   Name sheet
    ActiveSheet.Name = "Payroll Week Ending"
    
    Application.ScreenUpdating = True
    
End Sub
So, to add/remove columns, simply update this line here:
Code:
    cols = Array("D", "T", "U")
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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