I've got a VBA module built to import a text file but need some help with one thing. The problem is, field 7 of the text file can contain commas and if commas are present, it's splitting the text up instead of leaving the commas in field 7. I also need to leave the double quotes in the data if they are present in the text file. Here is an example of the text file.
"1234","0240","AF-483",47.20,0.00,"NE0004","000, NEW, (ID GMP08A)",0,"","","WT6A112","ma29",1,"99","W",3/3/16,"0000007021","A","",76034,0,"","0","","1",0,"","","","","","","","","","",,"",0,0.00,0.00,"","1097-ma29-76034","",0.00,"459D95A0","","",,"","","1097-ma29-VNE0004","",,"","",""
Here's my VBA code.
Sub ImportINVENT_CAP()
Dim fName As String, LastRow As Long
Sheets("INVENT.CAP").Select
Range("B14").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(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("Q14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("AX14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("BC14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Cells.Select
Selection.ColumnWidth = 250
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Range("B14").Select
End Sub
I tried changing the .TextFileTextQualifier = line to xlTextQualifierDoubleQuote and that fixed the issue with the commas in field 7 but now I'm missing the double quotes in the data.
Any help would be greatly appreciated.
"1234","0240","AF-483",47.20,0.00,"NE0004","000, NEW, (ID GMP08A)",0,"","","WT6A112","ma29",1,"99","W",3/3/16,"0000007021","A","",76034,0,"","0","","1",0,"","","","","","","","","","",,"",0,0.00,0.00,"","1097-ma29-76034","",0.00,"459D95A0","","",,"","","1097-ma29-VNE0004","",,"","",""
Here's my VBA code.
Sub ImportINVENT_CAP()
Dim fName As String, LastRow As Long
Sheets("INVENT.CAP").Select
Range("B14").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(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("Q14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("AX14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("BC14").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "mm/dd/yy;@"
Cells.Select
Selection.ColumnWidth = 250
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Range("B14").Select
End Sub
I tried changing the .TextFileTextQualifier = line to xlTextQualifierDoubleQuote and that fixed the issue with the commas in field 7 but now I'm missing the double quotes in the data.
Any help would be greatly appreciated.