VBA To Open File Based on Its Name

reddsable

New Member
Joined
Jun 3, 2019
Messages
12
Hi There, I want to open an Excel file based on this name. The name needs to end with the string "AB11.xlsx"

I tried this code, but the file is not found:

VBA Code:
Dim wb As Workbook
Dim SourceWorkbook As Workbook


Set wb = ThisWorkbook
Set SourceWorkbook = Workbooks.Open("C:\Users\Me\Desktop\Test\Split \*AB11.xlsx")

Any idea what I am doing wrong?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Will there only be one file in the folder that ends with AB11.xlsx?
 
Upvote 0
There are several files in my folder, and only one ends with AB11. After this file is found, i want to find the next file, this time with AB12 and so on..
 
Upvote 0
Ok, how about
VBA Code:
   Dim wb As Workbook, SourceWorkbook As Workbook
   Dim Pth As String, Fname As String
   
   Pth = "C:\Users\Me\Desktop\Test\Split \"
   Set wb = ThisWorkbook
   Fname = Dir(Pth & "*AB11.xlsx")
   If Fname = "" Then
      MsgBox "file not found"
      Exit Sub
   End If
   Set SourceWorkbook = Workbooks.Open(Pth & Fname)
 
Upvote 0
Ok, how about
VBA Code:
   Dim wb As Workbook, SourceWorkbook As Workbook
   Dim Pth As String, Fname As String
  
   Pth = "C:\Users\Me\Desktop\Test\Split \"
   Set wb = ThisWorkbook
   Fname = Dir(Pth & "*AB11.xlsx")
   If Fname = "" Then
      MsgBox "file not found"
      Exit Sub
   End If
   Set SourceWorkbook = Workbooks.Open(Pth & Fname)
That worked! Thank you so much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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