I'm trying to loop through .csv file, opened through FileDialog. The code throws a
run-time '1004' error
on the line:
Set searchInColumn = ActiveSheet.Cells(i, j).Offset(, -1).EntireColumn
In the process, the data from populated columns is being put into cells in the first column, separated by semicolons.
My idea is to run the code on .csv and .xlsx files. Code is shortened. Whatever code is below the problematic line, the whole Sub works on .xlsx, but gets stuck on .csv. It also goes well when I get rid of all the FileDialog stuff, put the code in .xlsb file, paste the data there (from .csv) and run the macro from alt+f8 list.
I run out of ideas to solve this, could you give me some hint?
Sub FixCSV()
Dim findMatch As Range, searchInColumn As Range
Dim i As Long, j As Long, k As Long, lastRow As Long, lastColumn As Long
Dim chosenFile As Integer
Dim chooseFiles As Office.FileDialog
Application.ScreenUpdating = False
Set chooseFiles = Application.FileDialog(msoFileDialogFilePicker)
With chooseFiles
.AllowMultiSelect = True
.Title = "Please select the file."
.InitialFileName = "c:\"
.InitialView = msoFileDialogViewList
End With
chosenFile = chooseFiles.Show
If chosenFile = -1 Then
For k = 1 To chooseFiles.SelectedItems.Count
Workbooks.Open chooseFiles.SelectedItems(k)
lastColumn = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
j = 2
Do Until j = lastColumn
Set searchInColumn = ActiveSheet.Cells(1, j).Offset(, -1).EntireColumn
Set findMatch = searchInColumn.Find(What:=ActiveSheet.Cells(1, j).Value)
If ActiveSheet.Cells(1, lastColumn).Offset(0, 1).Value = "" Then
j = j + 1
End If
Loop
Next k
End Sub