I'm building a macro / button to import a cap file (simply a txt file renamed to a a file extension of cap) that contains 68 fields, separated by commas. These fields are a mix of numeric and string values and some string values contain commas embedded. The string fields are wrapped in quotes.
What I'm trying to do is split the fields into 68 separate columns, ignoring commas that are in fields wrapped in quotes and retain the text qualifiers for string fields. Currently, I can retain the text qualifiers but if there are commas embedded in the string fields, it's splitting those fields on every comma, which is throwing off the columns.
Is this possible?
This is what I have thus far:
What I'm trying to do is split the fields into 68 separate columns, ignoring commas that are in fields wrapped in quotes and retain the text qualifiers for string fields. Currently, I can retain the text qualifiers but if there are commas embedded in the string fields, it's splitting those fields on every comma, which is throwing off the columns.
Is this possible?
This is what I have thus far:
VBA Code:
Sub ImportINVENT_CAP()
Dim fName As String, LastRow As Long
Sheets("INVENT.CAP").Select
Range("B18").Select
fName = Application.GetOpenFilename("Cap Files (*.cap), *.cap")
If fName = "False" Then Exit Sub
LastRow = Range("B" & Rows.Count).End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
Destination:=Range("B" & LastRow))
.Name = "sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With