Hi all,
Hope you are well and patient enough to go through this with me.
I am trying to create a multiple file picker. The code is below.
I want to open the first file of lets say 5 selected files, do some autofilter task, copy data from that file, paste it in another workbook and close it. Open second file, do some autofilter task, copy data from that file, paste it in below the first file pasted data in another workbook and close it... and File 3... and so on.
It works for one file, the code for copying pasting is not yet added. I think I can manage that. But how I return to the fileicker function after it opens the first file.
Private Sub Merge_Data_Click()
Dim csvWbkPath As String
csvWbkPath = FilePickerCSV()
If csvWbkPath = "Exit" Then
Sheets("Macro").Select
Exit Sub
ElseIf csvWbkPath = "" Then
MsgBox ("Aborting the process!")
Worksheets("Macro").Select
Range("A2").Select
Exit Sub
End If
csvWbkName = Right(csvWbkPath, Len(csvWbkPath) - InStrRev(csvWbkPath, "\"))
Workbooks.Open (csvWbkPath)
End Sub
Function FilePickerCSV(Optional initialPath As String = "", Optional title As String = "Please select a file", Optional filters As String = "")
Dim fDialog As Office.FileDialog
Dim selectedFile As String
Dim filter, filterName, filterExtn
Dim i As Integer
MSG1 = MsgBox("Please select the CSV file.", vbYesNo)
If MSG1 = vbYes Then
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
If initialPath > "" Then
.InitialFileName = initialPath
End If
.AllowMultiSelect = True
.title = title
.filters.Clear
.InitialFileName = "" 'clear the file box
If filters > "" Then
For Each filter In Split(filters, "|")
If filter > "" Then
i = InStr(filter, ",")
filterName = Left(filter, i - 1)
filterExtn = Mid(filter, i + 1)
.filters.Add filterName, filterExtn
End If
Next
Else
.filters.Add "All files", "*.CSV"
End If
Dim fl As Integer
If .Show = True Then
'selectedFile = .SelectedItems(5)
'open each of the files chosen
For fl = 1 To .SelectedItems.Count
THIS IS WHERE I AM OPENING THE SELECTED FILE. i WANT TO RETURN TO THE MAIN FUNCTION AND DO COPY/PASTE/CLOSE AND COME HERE AGAIN TO GO THROUGH THE SECOND FILE.
Workbooks.Open .SelectedItems(fl)
selectedFile = .SelectedItems(fl)
FilePickerCSV = selectedFile
Next fl
End If
End With
FilePickerCSV = selectedFile
Else
FilePickerCSV = "Exit"
End If
End Function
Many thanks,
rajwar
Bookmarks