Text to Columns selection and cell formatting

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
Hi all,

Currently I have the code below, which converts text to columns. I paste the text (with multiple rows) in cell A1 and run the macro.

Code:
Sub TextToColumns()
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(8, 1), Array(25, 1), Array(73, 1), Array(85, 1), _
        Array(88, 1), Array(104, 1)), TrailingMinusNumbers:=True
Columns("B:B").Select
    Selection.NumberFormat = "0.00"
    Selection.NumberFormat = "0.0"
    Selection.NumberFormat = "0" 


End Sub

I would like to have a macro that converts text to columns, no matter where I paste the text. Then I select the rows with text which needs to be converted to columns. The macro should convert the text within the selection. The second column should be formatted as standard.


So a practical example:
I paste text in cells B4 to B10. Then I select B4 to B10 and run the macro.
The text should be formatted to columns and cell C4 to C10 should be formatted as standard.

Thanks in advance.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How about
Code:
Sub TextToColumns()
    Selection.TextToColumns Destination:=Selection, DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(8, 1), Array(25, 1), Array(73, 1), Array(85, 1), _
        Array(88, 1), Array(104, 1)), TrailingMinusNumbers:=True
    Selection.Offset(, 1).NumberFormat = "0"
End Sub
 
Upvote 0
How about
Code:
Sub TextToColumns()
    Selection.TextToColumns Destination:=Selection, DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(8, 1), Array(25, 1), Array(73, 1), Array(85, 1), _
        Array(88, 1), Array(104, 1)), TrailingMinusNumbers:=True
    Selection.Offset(, 1).NumberFormat = "0"
End Sub

Thanks! it works!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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