Tony Nguyen
Tony Nguyen

Reputation: 73

VBA - open a single .csv file in a folder

I am trying to open a .csv using the following code, but it doesn't seem to work. My file is saved in a Downloads folder and there is only 1 file in that folder. Because the file name changes over, so I use the wildcard.

I am using the following code.

Workbooks.Open "C:\Users\" & Environ$("username") & "\Downloads\" & "*.csv"

Upvotes: 1

Views: 930

Answers (1)

Andreas
Andreas

Reputation: 23958

Use dir() to find the filename, then open using workbook.open.

Fname = Dir("C:\Users\" & Environ$("username") & "\Downloads\" & "*.csv")
Workbooks.Open "C:\Users\" & Environ$("username") & "\Downloads\" & Fname

Or combine the two:

Workbooks.Open "C:\Users\" & Environ$("username") & "\Downloads\" & Dir("C:\Users\" & Environ$("username") & "\Downloads\" & "*.csv")

Upvotes: 2

Related Questions