VBA to Find CSV Files in Folder by Current Month

CreativeUsername

Board Regular
Joined
Mar 11, 2017
Messages
52
Hi,

I'm trying to get VBA to find the current month folder on a given file path then open all ".csv" items in that folder. I had code that die something similar with in a given folder. Now the folder is a variable. If I put "March" in the file path it finds it and opens it obviously. What want to do is swap "March" with a "ThisMonth" variable. but I'm missing something. Several versions haven't worked.

Code:
Sub OpenCSVs()
Dim MyFiles As String, ThisMonth As String
ThisMonth = Month(Date,"mmmm")
MyFiles = Dir("C:\Users\ME\Desktop\CSV find convert tests\ThisMonth\*.csv")
Do While MyFiles <> ""
Workbooks.Open "C:\Users\ME\Desktop\CSV find convert tests\ThisMonth\" & MyFiles
'ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Loop
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Sub OpenCSVs()
Dim MyFiles As String, ThisMonth As String
ThisMonth = Month(Date,"mmmm")
MyFiles = Dir("C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\*.csv")
Do While MyFiles <> ""
Workbooks.Open "C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\" & MyFiles
'ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Loop
End Sub
 
Upvote 0
To break up a string and insert a variable "this is a string " & this_is_a_variable & " and continue string. "
the & to join in the variable into the string.
the change from the original code is marked in red.


Code:
Sub OpenCSVs()
Dim MyFiles As String, ThisMonth As String
   ThisMonth = Month(Date,"mmmm")
   MyFiles = Dir("C:\Users\ME\Desktop\CSV find convert tests\[COLOR=#ff0000]" & ThisMonth & "[/COLOR]\*.csv")
   Do While MyFiles <> ""
       Workbooks.Open "C:\Users\ME\Desktop\CSV find convert tests\[COLOR=#ff0000]" & ThisMonth & "[/COLOR]" & MyFiles
        'ActiveWorkbook.Close SaveChanges:=True
       MyFiles = Dir
   Loop
End Sub
 
Last edited:
Upvote 0
The following works:
Code:
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
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top