Article ID: 134826
Article Last Modified on 10/11/2006
Sub OpenText_Ex1()
Dim ColumnArray(1 To 100, 1 To 2) As Integer
Dim x As Integer
' Create a For-Next that populates the two dimensions of
' the ColumnArray array.
For x = 1 To 100
ColumnArray(x, 1) = x
ColumnArray(x, 2) = 1
Next x
' Open the delimited text file using ColumnArray as the
' FieldInfo parameter.
Workbooks.OpenText _
Filename:="C:\TEST.TXT", DataType:=xlDelimited, _
FieldInfo:=ColumnArray
End Sub
If you want to open a text file and explicitly define specific
columns as a certain data type, create one array that contains
the columns you want to explicitly define and one that contains
the data type of those columns. Columns that are not explicitly
defined will be opened with the General data type, which is the
default.
Sub OpenTextFile_Ex2()
Dim ColumnsDesired
Dim DataTypeArray
Dim ColumnArray(0 To 5, 1 To 2)
' Define the specific column information in two arrays.
ColumnsDesired = Array(1, 2, 3, 4, 99, 100)
DataTypeArray = Array(9, 3, 3, 9, 3, 2)
' Create a For-Next that populates the two dimensions of
' the ColumnArray array.
For x = LBound(ColumnsDesired) To UBound(ColumnsDesired)
ColumnArray(x, 1) = ColumnsDesired(x)
ColumnArray(x, 2) = DataTypeArray(x)
Next x
' Open the delimited text file using ColumnArray as the
' FieldInfo parameter.
Workbooks.OpenText _
Filename:="C:\TEST.TXT", DataType:=xlDelimited, _
FieldInfo:=ColumnArray
End Sub
Sub OpenTextFile_Ex3()
Dim ColumnSizes
Dim DataTypeArray
Dim ColumnArray(0 To 5, 1 To 2)
' Define the specific column information in two arrays.
ColumnSizes= Array(3, 8, 12, 16, 20, 24)
DataTypeArray = Array(1, 1, 1, 1, 1, 9)
' Create a For-Next that populates the two dimensions of
' the ColumnArray array.
For x = LBound(ColumnSizes) To UBound(ColumnSizes)
ColumnArray(x, 1) = ColumnSizes(x)
ColumnArray(x, 2) = DataTypeArray(x)
Next x
' Open the fixed width text file using ColumnArray as the
' FieldInfo parameter.
Workbooks.OpenText _
Filename:="C:\TEST.TXT", DataType:=xlFixedWidth, _
FieldInfo:=ColumnArray
End Sub
Additional query words: OpenText Parse XL97 XL7 XL5 XL
Keywords: kbprogramming KB134826