Macro to open files with varying names

Matt

Board Regular
Joined
Feb 16, 2002
Messages
212
I'm looking for some code that will open a csv file on a daily basis that has the same prefix but the last part of the filename changes. The last part of the filename is the current date, in mmdd format e.g. 0322

What I have tried so far has failed dismally, can anybody help?

Workbooks.Open FileName:= _
"C:testtest01 " & text(Date"mmdd") & ".csv"

thanks

Matt
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I think the syntax you have is slightly wrong.
I first tried the following...

Workbooks.Open Filename:= _
"C:testtest01 " & Month(Date) & Day(Date) & "csv"

The problem with this, is that it treats March as 3 rather than 03 with the leading zeros. Therefore, if you have the following, inserting the leading zeros where necessary for months and days, it should work.

If Month(Date) < 10 And Day(Date) < 10 Then

Workbooks.Open Filename:= _
"C:testtest01 0" & Month(Date) & "0" & Day(Date) & ".csv"

ElseIf Month(Date) < 10 And Day(Date) > 9 Then

Workbooks.Open Filename:= _
"C:testtest01 0" & Month(Date) & Day(Date) & ".csv"

ElseIf Month(Date) > 9 And Day(Date) < 10 Then

Workbooks.Open Filename:= _
"C:testtest01 " & Month(Date) & "0" & Day(Date) & ".csv"

Else

Workbooks.Open Filename:= _
"C:testtest01 " & Month(Date) & Day(Date) & ".csv"

End If

I'm sure there's a better way though - this is very cumbersome!

Rgds
AJ
 
Upvote 0
You could try the following: -

Workbooks.Open FileName:= _
"C:testtest01 " & Format(Date, "mmdd")& ".csv"
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,576
Members
448,972
Latest member
Shantanu2024

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