strider_svr
New Member
- Joined
- Nov 19, 2016
- Messages
- 3
I parsed the first row of the text file to create two arrays:
I fill the arrays with starting position of each column (positions) and the column format (position_format). Next i import the text file with Fixed width positions from step 1. The code is as follows:
The first column gets delimited correctly, but the other columns do not. I checked the .TextFileColumnDataType and .TextFileFixedColumnWidths. The arrays are correctly assigned (as far as i can tell - i did try using Double as my array variables - no difference):
I cannot figure out why the actual data when placed in worksheet is not split into correct column widths. Second pair of eyes would be greatly appreciated.
Code:
Dim positions() As Long
Dim position_format() As Long
Code:
Sub importTextFile(positions() As Long, position_format() As Long)
Dim ws As Worksheet
Dim qt_Data As QueryTable
Set ws = Worksheets("Sheet2")
ws.Cells.Delete
Set qt_Data = ws.QueryTables _
.Add(Connection:="TEXT;C:\Users\tmp\Desktop\tmp\sample.txt", _
Destination:=ws.Cells(1, 1))
With qt_Data
.FieldNames = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileColumnDataTypes = position_format ' tried putting position_format with Array(position_format)
.TextFileFixedColumnWidths = positions ' tried putting positions with Array(positions)
.TextFileTrailingMinusNumbers = True
.Refresh
End With
Set qt_Data = Nothing
ActiveWorkbook.Connections(ActiveWorkbook.Connections.Count).Delete
End Sub
The first column gets delimited correctly, but the other columns do not. I checked the .TextFileColumnDataType and .TextFileFixedColumnWidths. The arrays are correctly assigned (as far as i can tell - i did try using Double as my array variables - no difference):


I cannot figure out why the actual data when placed in worksheet is not split into correct column widths. Second pair of eyes would be greatly appreciated.