How to Search Directory for An incomplete file name in VBA

powershot32

New Member
Joined
Aug 26, 2014
Messages
18
Hey everyone. I am trying to write code for a macro in VBA to search my directory and open a different excel spreadsheet. The basics of the macro would be taking the ActiveCell and searching a folder for a file that contains that value and opening the excel spreadsheet. I am struggling with how I can use the string value for the ActiveCell and insert it into either Dir() function or other search functions.

Currently I have

Dim SearchValue As String
SearchValue = ActiveCell.Value

The values entered in the activecell would be something like A12345 and the file names in the directory are like A12345 and then a bunch more descriptive numbers and words afterwords.

So then I have

Dir:)\filepath & SearchValue*.xls)

But this obviously isn't working as it is just searching the directory for any files called SearchValue. I can't figure out how to put the string value in the Dir function.

Also there are subfolders within the directory that could potentially hold the file too so is there a better function to search and open the file or should I just run a loop with more Dir() functions with the subfolders until the file is found?

Thanks for any help.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try:

Rich (BB code):
Dir(:\filepath & SearchValue & "*.xls")
That is just returning the first file within the directory and not one specific to the value entered in the activecell.

Also I forgot to add I have

Dim ActualValue As String

and then the Dir function is

ActualValue = Dir(":filepath" & SearchValue*.xls)
Workbooks.Open (ActualValue)

But it keeps returning Run-Time Error '1004':

Sorry, we couldn't find file.xlsx
 
Last edited:
Upvote 0
Please post your exact VBA code.

Sub CEFile()


Dim SearchValue As String, ActualValue As String
SearchValue = ActiveCell.Value
ActualValue = Dir("K:\PMT\Computers & Electronics\" & SearchValue*.xls)
Workbooks.Open (ActualValue)

End Sub

If I put the " " around SearchValue*.xls it doesn't return anything. I am trying to get the string value inserted into that part of the Dir function.
 
Upvote 0
It's:

Rich (BB code):
ActualValue = Dir("K:\PMT\Computers & Electronics\" & SearchValue & "*.xls")

Ok so that worked and found the correct file, but now the Workbooks.Open is giving me the runtime error of '1004. It says it couldn't find the file but it is still in the directory. Is that the correct way to open the file or is something else amiss?
 
Upvote 0
I'm wondering if it is trying to find a file with .xls.xls instead of .xls. The error says

Run-time error '1004':

Sorry, we couldn't find FILE.xls. Is it possible it was moved, renamed or deleted?

So does that mean its looking for a file with the name FILE.xls.xls? or is it just looking for FILE.xls.
 
Upvote 0

Forum statistics

Threads
1,214,660
Messages
6,120,787
Members
448,994
Latest member
rohitsomani

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