Mr.Riply
Mr.Riply

Reputation: 845

Opening an excel file with partial name

Is there a way to open an excel file without knowing the full path name?

For example: TEST_03222018.csv is the file name located in C:\test\folder

the known part of the string\path is

C:\test\folder\TEST_03

is there a way to open this csv sheet without the rest of the path (preferably without using InStr() or any If, While loops

Upvotes: 1

Views: 2623

Answers (2)

user4039065
user4039065

Reputation:

Use Dir with a wildcard to confirm the existence and if found, open it.

dim fp as string, fn as string

fp = "C:\test\folder\"
fn = "test_03"

fn = dir(fp & fn & "*.csv")

if cbool(len(fn)) then
    workbooks.open fp & fn, delimiter:=","
end if

Upvotes: 2

Nathan_Sav
Nathan_Sav

Reputation: 8531

Function findFile(strFileStart as string) as string

findFile= Dir(strFileStart & "*", vbNormal)

End Function

Echo, @Ryan Wilson's comments about having more than one file with the same prefix though.

Upvotes: 4

Related Questions