QueryTable.TextFileFixedColumnWidths Property (Excel)
Published: July 16, 2012
Returns or sets an array of integers that correspond to the widths of the columns (in characters) in the text file that you’re importing into a query table. Valid widths are from 1 through 32767 characters. Read/write Variant.
Use this property only when your query table is based on data from a text file (with the QueryType property set to xlTextImport), and only if the value of the TextFileParseType property is xlFixedWidth.
You must specify a valid, nonnegative column width. If you specify columns that exceed the width of the text file, those values are ignored. If the width of the text file is greater than the total width of columns you specify, the balance of the text file is imported into an additional column.
If you import data using the object model, data from a Web query or a text query must be imported as a QueryTable, while all other external data can be imported as either a ListObject or a QueryTable.
The TextFileFixedColumnWidths property applies only to QueryTable objects.
This example imports a fixed-width text file into a new query table on the first worksheet in the first workbook. The first column in the text file is five characters wide and is imported as text. The second column is four characters wide and is skipped. The remainder of the text file is imported into the third column and has the General format applied to it.
Set shFirstQtr = Workbooks(1).Worksheets(1) Set qtQtrResults = shFirstQtr.QueryTables _ .Add(Connection := "TEXT;C:\My Documents\19980331.txt", _ Destination := shFirstQtr.Cells(1, 1)) With qtQtrResults .TextFileParseType = xlFixedWidth .TextFileFixedColumnWidths = Array(5, 4) .TextFileColumnDataTypes = _ Array(xlTextFormat, xlSkipColumn, xlGeneralFormat) .Refresh End With