Within the code attached below, macro opens .csv with values separated by semicolon and comma (at least it looks like it's doing so), despite comma speparator set to false and semicolon separator set to true.
I tried to switch the order of pieces of the code, but to no avail. The history and the purpose of the code can be found in this thread: VBA run-time error '1004' while looping through .csv file. Please advise what I am doing wrong or missing here?
(Topic associated with this question.)
Dim wrk As Workbook
Dim Sh As Worksheet
Dim findMatch As Range, searchInColumn As Range
Dim i As Long, j As Long, k As Long
Dim chosenFile As Integer
Dim xlFileName As String
Dim chooseFiles As Office.FileDialog
Set DictChanged = CreateObject("Scripting.Dictionary")
Set DictToBeDone = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
Set chooseFiles = Application.FileDialog(msoFileDialogFilePicker)
With chooseFiles
.AllowMultiSelect = True
.Title = "Please select the file."
.InitialFileName = "c:\"
.InitialView = msoFileDialogViewList
.Filters.Add "All", "*.*"
End With
If chooseFiles.Show = -1 Then
For k = 1 To chooseFiles.SelectedItems.Count
xlFileName = chooseFiles.SelectedItems(k)
Set wrk = Application.Workbooks.Open(xlFileName)
Set Sh = wrk.Worksheets(1)
If InStr(1, wrk.Name, ".csv") Then
Sh.Range(Range("A1"), Range("A1").End(xlDown)).TextToColumns _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False
End If
End If
The described problems occur within the visible code. Below output stages as requested:
Output stages:
Initial File
+---------+---------+---------+------------------+------------+-----------------+ | column1 | column2 | column3 | column4 | column5 | column6 | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654321 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654322 | SomeID | Values,WithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654323 | SomeID | Values,WithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654324 | SomeID | Val,uesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654325 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654326 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654327 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+------------------+------------+-----------------+ | 123456 | 654328 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues |
2.AfterPickingFileInFileDialog
+-----------------------------------------------------------------+-----------------------------------------+ | column1;column2;column3;column4;column5;column6 | column2 | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654321;SomeID;ValuesWithComma;SomeValues;SomeOtherValues | 654321 | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654322;SomeID;Values | WithComma;SomeValues;SomeOtherValues | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654323;SomeID;Values | WithComma;SomeValues;SomeOtherValues | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654324;SomeID;Val | uesWithComma;SomeValues;SomeOtherValues | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654325;SomeID;ValuesWithComma;SomeValues;SomeOtherValues | 654325 | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654326;SomeID;ValuesWithComma;SomeValues;SomeOtherValues | 654326 | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654327;SomeID;ValuesWithComma;SomeValues;SomeOtherValues | 654327 | +-----------------------------------------------------------------+-----------------------------------------+ | 123456;654328;SomeID;ValuesWithComma;SomeValues;SomeOtherValues | 654328 | +-----------------------------------------------------------------+-----------------------------------------+
3.AfterLastCondition
+---------+---------+---------+-----------------+------------+-----------------+ | column1 | column2 | column3 | column4 | column5 | column6 | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654321 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654322 | SomeID | Values | | | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654323 | SomeID | Values | | | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654324 | SomeID | Val | | | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654325 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654326 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654327 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+-----------------+------------+-----------------+ | 123456 | 654328 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues | +---------+---------+---------+-----------------+------------+-----------------+
TextToColumns
twice. My understanding is that you first open the file and it all appears in one column, then you call texttocolumns on that. You need to inspect the output at each stage and understand what is going on – Nick.McDermaid.csv
file than opening it. If you must open it, and it has non-standard delimiters (or date format that differs from your windows regional settings, then you can change the file type to.txt
. – Ron Rosenfeld*.csv
file (with sensitive information removed) to some public website and post a link in your original question – Ron Rosenfeld