0

I have a project to create VBA to find and open all CSV files in a folder that is named the current month. I found some stuff online that seemed close but not quite. They will eventually be converted to XLSX files and parsed. That part I have. The macro that converts, parses and saves will be housed in a different file along the same path but not as "deep".

So on my Desktop is a folder name "CSV find test". Inside are two folders "Feburary" and "March". I need it to open all csv files in the most recent month. I have the rest of the syntax. . . . .

I wouldn't imagine that it would take a huge amount of syntax. Thanks for any direction.

Sub OpenFile()

    FileMonth = Month(Date)
    FileDate = Format(Date, "mmmm")
    FilePath = "C:\Users\Me\Desktop\CSV find convert tests\" & FileMonth & "\" & FileDate & ".xls"

    Workbooks.Open Filename:="FilePath" <- - -  - error happens here.

End Sub
4
  • 2
    FilePath is a variable, not a string. Remove the double-quotes. Commented Mar 21, 2017 at 21:33
  • 2
    Also if you're looking for CSV files your extension is wrong... See here for how to loop over all CSV files in a folder: stackoverflow.com/questions/25163369/… Commented Mar 21, 2017 at 21:37
  • 2
    1. Use FileMonth = Format(Date, "mmmm") to get the full month name. 2. Use Debug.Print FilePath before you try to open the CSV and look in hte VBE's Immediate window to make sure it is correct. 3. As above. Commented Mar 21, 2017 at 22:10
  • Oops. was in a rush. had several versions I was tinkering with and then copied the oldest here. Obviously it should be ".csv" Commented Mar 22, 2017 at 13:09

1 Answer 1

1

I don't think you really understand how a variable works as you keep putting it in a string. If you put something in double quotes it creates a string. Below is how you can add the month to the string via the variable.

Sub OpenCSVs()
Dim MyFiles As String, ThisMonth As String
Dim startPath As String
ThisMonth = Format(Date, "mmmm")
startPath = "C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\"
MyFiles = Dir(startPath & "*.csv")

Do While MyFiles <> ""

Workbooks.Open startPath & MyFiles

       'Do stuff to it will go here
'ActiveWorkbook.Close SaveChanges:=True (Deactivated for now)

MyFiles = Dir

Loop

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Oh I see... thanks so much. The above worked perfectly.
so I recorded a Macro to parse out the CSV data. I'm trying to save the file and convert it to xlsx format. I want to preserve the CSV file name which was simply a date. I found all sorts of stuff on line but none seems to work. rec. Macro sets the file name.
Convert = Dir(startPath & "*.xlsx") 'FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False ActiveWorkbook.SaveAs startPath & Convert Not working. . . .:/
@ScottH I would create a new question and post the code portion there so not to get the two issues confused in case someone comes here to review this one.

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.