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
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