open file with wildcard or partial file name

sheppy72

Board Regular
Joined
Jun 7, 2006
Messages
104
I am trying to try and get my macro to work but struggling. Any help please.

in the same dir as where my sheet with the code is I save a files. They have different names and I want to be able to open the correct file to work on. The file names can be long but they tend to have some short easy to remember model codes.
I would like therefore to be able to just type in *x123* in to the inputbox and this would open the file test_macro_x123_test.xls, or if i type L888 it would open the file test_macro_L888_test.xls.

Dim filename1 As String
filename1 = InputBox("enter model code")
Workbooks.Open filename:=ThisWorkbook.Path & "\" & filename1 & ".xls"
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Rather than that, how about the user physically locates the file to use via the same dialog interface you would use to open a file in Excel via File>Open?
 
Upvote 0
The sheet that the code is on is a sheet that has several macros that then need to be run. The workbook that the input box refers to is the sheet with the data on and many many sheets. All these sheets are used and called for in my code currently.
 
Upvote 0
OK you could use Dir for this eg:

Code:
Dim filename1 As String, strFile As String
Dim wb As Workbook


filename1 = InputBox("enter model code")

'find matching file in directory:
strFile = Dir(ThisWorkbook.Path & "\*" & filename1 & "*.xls")

'check filename found:
If strFile<>"" Then   'if it isn't "" then strFile holds the name (not path) of matching file
   Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & strFile)
Else
  MsgBox "No matching file found! Exiting Sub..."
  Exit Sub
End If

'you now have the workbook open and a reference set to it so you can do stuff with it eg:

wb.Sheets(1).Activate
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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