Home » excel » excel – VBA – open a single .csv file in a folder

excel – VBA – open a single .csv file in a folder

Posted by: admin May 14, 2020 Leave a comment

Questions:

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"
How to&Answers:

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")