VBA userform-Select multiple workbooks with diff names through browse dialogue.

THE_NEW_XL_GUY

New Member
Joined
Dec 20, 2017
Messages
47
Hi guys,

Need a way to select multiple csv/xls files with different names through browse function.

code I have now:

Code:
Fname = Application.GetOpenFilename(FileFilter:="XLS Files (*.xls),*.xls,CSV Files (*.csv),*.csv", Title:="Select a file or files",
            MultiSelect:=True)

I have around 10 files with different names, names way vary slightly each time I browse.

All I want to know is there a way to search the folder with keywords of file and then open the files and perform action specified with that keyword,
close the opened 10 files. I have 10 macros to be performed on them


thanks in advance.
 
Code:
fNameAndPath = Application.GetOpenFilename(fileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened", MultiSelect:=True)
    If Not IsArray(fNameAndPath) Then Exit Sub

    For Each MyFile In fNameAndPath
        Set wb = Workbooks.Open(MyFile)==========(how to search for specific file name in myfile array.. 
        
        ' do stuff with workbook that has been opened

          if myfile= test1.csv then
             


          elseif myfile=test2.csv then
            'do my macro2

          elseif myfile=test3.csv then
            'do my macro3

end if.

        
        wb.Close SaveChanges:=False
        
    Next MyFile
    
End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
updated code in macro1 place

Code:
fNameAndPath = Application.GetOpenFilename(fileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened", MultiSelect:=True)
     If Not IsArray(fNameAndPath) Then Exit Sub

     For Each MyFile In fNameAndPath
         Set wb = Workbooks.Open(MyFile)==========(how to search for specific file name in myfile array.. 
         
         ' do stuff with workbook that has been opened

           if myfile= test1.csv then
              
  
               Set Fnd1 = Range("A1")
          Qty1 = WorksheetFunction.CountIf(Rows(1), "*shop3**high*")
    For Cnt1 = 1 To Qty1
       Set Fnd1 = Rows(1).Find("*shop3**high*",Fnd1, , xlWhole, , , False, , False)
       max_num = Application.WorksheetFunction.Max(Fnd1.EntireColumn)    ' maxnum value is copied to a cell in submit button
    Next Cnt1

           elseif myfile=test2.csv then
             'similar to macro1

           elseif myfile=test3.csv then
             similar to macro1

 end if.

         
         wb.Close SaveChanges:=False
         
     Next MyFile
     
 End Sub

kindly correct syntax or typo..
 
Upvote 0
@Norie anything am missing?

Code:
fNameAndPath = Application.GetOpenFilename(fileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened", MultiSelect:=True)
     If Not IsArray(fNameAndPath) Then Exit Sub

     For Each MyFile In fNameAndPath
         Set wb = Workbooks.Open(MyFile)==========(how to search for specific file name in myfile array.. 
         
         ' do stuff with workbook that has been opened

           if myfile= test1.csv then
              
  
               Set Fnd1 = Range("A1")
          Qty1 = WorksheetFunction.CountIf(Rows(1), "*shop3**high*")
    For Cnt1 = 1 To Qty1
       Set Fnd1 = Rows(1).Find("*shop3**high*",Fnd1, , xlWhole, , , False, , False)
       max_num = Application.WorksheetFunction.Max(Fnd1.EntireColumn)    ' maxnum value is copied to a cell in submit button
    Next Cnt1

           elseif myfile=test2.csv then
             'similar to macro1

           elseif myfile=test3.csv then
             similar to macro1

 end if.

         
         wb.Close SaveChanges:=False
         
     Next MyFile
     
 End Sub
 
Upvote 0
Hi @Norie
Am still stuck with here, really appreciate some help from you on the below

Code:
 fNameAndPath = Application.GetOpenFilename(fileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened", MultiSelect:=True)
     If Not IsArray(fNameAndPath) Then Exit Sub

     For Each MyFile In fNameAndPath
         Set wb = Workbooks.Open(MyFile)==========(how to search for specific file name in myfile array.. 
         
         ' do stuff with workbook that has been opened

           if myfile= test1.csv then
              
  
               Set Fnd1 = Range("A1")
          Qty1 = WorksheetFunction.CountIf(Rows(1), "*shop3**high*")
    For Cnt1 = 1 To Qty1
       Set Fnd1 = Rows(1).Find("*shop3**high*",Fnd1, , xlWhole, , , False, , False)
       max_num = Application.WorksheetFunction.Max(Fnd1.EntireColumn)    ' maxnum value is copied to a cell in submit button
    Next Cnt1

           elseif myfile=test2.csv then
             'similar to macro1

           elseif myfile=test3.csv then
             similar to macro1

 end if.

         
         wb.Close SaveChanges:=False
         
     Next MyFile
     
 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,156
Members
452,385
Latest member
Dottj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top