VBA Check if Partial File Name Exists

davidb88

Board Regular
Joined
Sep 23, 2013
Messages
76
Hi - I am trying to write a macro that will open a workbook if it exists. The workbook that will be opening always starts with the same characters, but the end of the file name varies based on date. I was trying to include a "wildcard" character to account for this but I keep getting a run time error 1004 saying the file does not exist. Would someone be able to verify the code below and see where I am maybe going wrong?

Thank you.

Code:
Sub BusinessObjectsRetrieve()


Dim strFile As String
Dim WB As Workbook
Dim dirFile As String


strFile = "AutoRunQuery_" & "*"
dirFile = "C:\Users\user\Documents\AutoRunQuery*.xlsx"




If Len(Dir(dirFile)) = 0 Then
    MsgBox "File does not exist!"
Else


Set WB = Workbooks.Open(dirFile)
End If


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Does this work?
Code:
Sub BusinessObjectsRetrieve()

Dim pStr As String
Dim strFile As String
Dim WB As Workbook
Dim dirFile As String

pStr = "C:\Users\user\Documents\"
strFile = "AutoRunQuery_" & "*" & ".xlsx"
dirFile = Dir(pStr & strFile)




If Len(dirFile) = 0 Then
    MsgBox "File does not exist!"
Else


Set WB = Workbooks.Open(dirFile)
End If


End Sub
 
Last edited:
Upvote 0
Thanks for your reply, Joe. I am still getting an error on that one. I tried a couple message boxes to see what was being captured in those variables and it looks like it is identifying the correct file in the dirFile, but the problem is that the file path is not getting captured anywhere so when it goes to open DirFile I don't think it knows where to look. Or maybe I'm thinking about it wrong? Thanks again.
 
Upvote 0
Thanks for your reply, Joe. I am still getting an error on that one. I tried a couple message boxes to see what was being captured in those variables and it looks like it is identifying the correct file in the dirFile, but the problem is that the file path is not getting captured anywhere so when it goes to open DirFile I don't think it knows where to look. Or maybe I'm thinking about it wrong? Thanks again.
Is pStr the correct file path?
 
Upvote 0
I was able to figure it out. Needed to add the pSTR to the beginning of the DIR(pSTR & dirFile). Thanks again for your help!

Code:
Sub BusinessObjectsRetrieve()


Dim pSTR As String
Dim strFile As String
Dim WB As Workbook
Dim dirFile As String


pSTR = "C:\Users\user\Documents\"
strFile = "AutoRunQuery_" & "*" & ".xlsx"
dirFile = Dir(pSTR & strFile)


Set WB = Workbooks.Open(pSTR & dirFile)


End Sub
 
Upvote 0
I was able to figure it out. Needed to add the pSTR to the beginning of the DIR(pSTR & dirFile). Thanks again for your help!

Code:
Sub BusinessObjectsRetrieve()


Dim pSTR As String
Dim strFile As String
Dim WB As Workbook
Dim dirFile As String


pSTR = "C:\Users\user\Documents\"
strFile = "AutoRunQuery_" & "*" & ".xlsx"
dirFile = Dir(pSTR & strFile)


Set WB = Workbooks.Open(pSTR & dirFile)


End Sub
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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