Column format for VBA text to column

gt213

Board Regular
Joined
Jul 5, 2016
Messages
61
I am using VBA to import some data from TXT files to excel, depending on the name of txt file it is either fixed width or comma delimited.

For both examples I need to be set the format of certain columns to text rather than general that it defaults to.

If it helps here is the relevant bit of code. In the first case it is Array(8,1) that I need as text, and in the second case it is data that is being pulled into the 5th and 6th columns (E&F in my worksheets).

rng.TextToColumns Destination:=rng, DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(8, 1), Array(18, 1), Array(31, 1), Array(41, 1), _
Array(53, 1), Array(61, 1), Array(72, 1), Array(86, 1), Array(96, 1), Array(106, 1), Array( _
116, 1), Array(118, 1), Array(120, 1), Array(122, 1), Array(124, 1), Array(126, 1), Array( _
128, 1), Array(140, 1), Array(167, 1), Array(169, 1), Array(201, 1), Array(205, 1), Array( _
255, 1), Array(262, 1), Array(307, 1), Array(321, 1), Array(331, 1), Array(341, 1), Array( _
351, 1), Array(361, 1), Array(371, 1), Array(381, 1), Array(391, 1), Array(401, 1), Array( _
411, 1), Array(421, 1), Array(431, 1), Array(441, 1), Array(451, 1), Array(461, 1), Array( _
471, 1), Array(481, 1), Array(491, 1), Array(501, 1), Array(511, 1)), _
TrailingMinusNumbers:=True
ActiveSheet.Columns("A:AZ").AutoFit

Else

rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:AZ").AutoFit
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Thanks Joe, I'll bear in mid trying the macro recorder more often for potential solutions.
 
Upvote 0
You are welcome!

Yes the Macro Recorder is a great little tool for getting snippets of code. Even us "experts" use it from time-to-time.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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