sunshine25
New Member
- Joined
- Dec 14, 2020
- Messages
- 3
- Office Version
- 2007
- Platform
- Windows
Hi Guys,
I am trying to import a .txt file that will have comma as a delimiter. However one of the fields has a comma (that is not a delimiter) in it. For example 'Cleo will sing, and play the banjo.' I have code that imports it but then it puts the 'and play the banjo' into the next field over. Below is the code:
I am trying to import a .txt file that will have comma as a delimiter. However one of the fields has a comma (that is not a delimiter) in it. For example 'Cleo will sing, and play the banjo.' I have code that imports it but then it puts the 'and play the banjo' into the next field over. Below is the code:
VBA Code:
Sub browseFilePath()
'-----------------------------------------
'Import System Data
Dim rg As Range
Dim xAddress As String
Dim ActSheet As String
On Error GoTo err
Dim fileExplorer As FileDialog
Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
Dim xFileName As Variant
Dim Answer As Integer
Dim c As Long
Dim splitstring As String
Dim myarray() As String
'To allow or disable to multi select
fileExplorer.AllowMultiSelect = False
With fileExplorer
.Filters.Clear
.Filters.Add "TXT Files", "*.txt"
If .Show = -1 Then 'Any file is selected
Cells(2, 41).Value = .SelectedItems.Item(1)
xFileName = Cells(2, 41).Value
'Create the CSV and import the file
ActSheet = "System Data"
If sheetExists(ActSheet) Then
Answer = MsgBox("Are you sure you want to Replace the existing sheet?", vbYesNo + vbQuestion, "Empty Sheet")
If Answer = vbYes Then
Worksheets(ActSheet).Delete
Else
Exit Sub
End If
End If
Sheets.Add(After:=Sheets(2)).Name = ActSheet
Sheets(ActSheet).Cells.Clear
Sheets(ActSheet).Activate
Set rg = Worksheets(ActSheet).Cells(1, 1)
xAddress = rg.Address
With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range(xAddress))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Else ' else dialog is cancelled
MsgBox "You have cancelled the dialogue"
Cells(6, 3).Value = "" ' when cancelled set blank as file path.
End If
End With
err:
Exit Sub
End Sub
Last edited by a moderator: