1

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"
4
  • You cant just do the * wildcard, look at dir Commented Dec 2, 2019 at 12:26
  • The code looks completely alright . Wildcard will pick the first file on the directory. Have you checked whether the path exists ? Could you explain about the error a little bit ? Commented Dec 2, 2019 at 12:29
  • Hi manoj0709, it said "Sorry, we couldn't find...., is it possible it was moved,...". I tried with a file specific directory then it works, but the error comes up when I change the file name to a wildcard *.csv Commented Dec 2, 2019 at 12:35
  • You can use Environ$("userprofile") instead of "C:\Users\" & Environ$("username") Commented Dec 2, 2019 at 13:02

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.