Open file based on Partial file name

Aswinraj

Board Regular
Joined
Dec 10, 2015
Messages
65
Hello guys,

I written a code where it will open a file based on Prefix of a file name. But I need to open a file based on certain file name matches.
Example: If file name is RSNOS_20200906_Summary and if RSNOS_Summary matches with the file name then macro should open the file.
If file name is XYZ_20200906_datasheet and if XYZ_datasheet matches with the file name then macro should open the file.

Please someone help on this.

VBA Code:
Sub Openfile()
Dim Fso, fs, Fl
Dim pvtTbl As PivotTable
    
    Set Fso = CreateObject("Scripting.FileSystemObject")
    'Application.ScreenUpdating = False
    For Each Fl In Fso.getfolder(ThisWorkbook.Path & "\").Files
        If (InStr("RSNOS", Left(Fl.Name, 5)) = 1) Then 'for rawdata file
            pa = ThisWorkbook.Path & "\"
            inputfile = Fl.Name
            Workbooks.Open (pa & inputfile)
        End If
    Next
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this

VBA Code:
Sub Openfile()
  Dim Fso As Object, fs, Fl As Variant
  Dim pvtTbl As PivotTable
  
  Set Fso = CreateObject("Scripting.FileSystemObject")
  Application.ScreenUpdating = False
  For Each Fl In Fso.getfolder(ThisWorkbook.Path & "\").Files
    If Fl.Name Like "RSNOS_*Summary*" Then  'for rawdata file
      Workbooks.Open ThisWorkbook.Path & "\" & Fl.Name
    End If
  Next
End Sub
 
Upvote 0
If I understood correctly
VBA Code:
If Fl.Name Like "RSNOS_*_Summary" or Fl.Name Like "XYZ_*_datasheet" Then
 
Upvote 0
Try this

VBA Code:
Sub Openfile()
  Dim Fso As Object, fs, Fl As Variant
  Dim pvtTbl As PivotTable
 
  Set Fso = CreateObject("Scripting.FileSystemObject")
  Application.ScreenUpdating = False
  For Each Fl In Fso.getfolder(ThisWorkbook.Path & "\").Files
    If Fl.Name Like "RSNOS_*Summary*" Then  'for rawdata file
      Workbooks.Open ThisWorkbook.Path & "\" & Fl.Name
    End If
  Next
End Sub

Thank you so Much.., it works :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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