Open csv file that has current date in the name VBA code

SantanaKRE8s

Board Regular
Joined
Jul 11, 2023
Messages
131
Office Version
  1. 365
Platform
  1. Windows
Hello can someone help, I am trying to automatically open a csv file that has current date on the name so I can transfer certain columns into my workbook. Below is the VBA code I have. I get error that the file could have been moved or deleted. I placed VBA code in Module 1

Sub OpenCSV()

Workbooks.Open "C:\Users\Bsantana\OneDrive - TTI, Inc\SPACEX\BACKLOGREPORT\OPEN ORDER REPORTS_SPX\SpaceX_Open_Order_Report_" & Format(Date, "mm-dd-yyyy") & ".csv"

End Sub


1696372101207.png
 
Thanks for confirming that.

Try this code and see if it returns the file name in a Message Box.
VBA Code:
Sub LoopThroughAllFilesInFolder()

    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String

    myPath = " C:\Users\Bsantana\OneDrive - TTI, Inc\SPACEX\BACKLOGREPORT\OPEN ORDER REPORTS_SPX\"
    myExtension = "*10-4-2023.csv"

    myFile = Dir(myPath & myExtension)
    Do While myFile <> ""
        If Len(myFile) = 0 Then Exit Do
        MsgBox myPath & myFile
        
        myFile = Dir
    Loop

End Sub

If not, try changing this line:
VBA Code:
    myExtension = "*10-4-2023.csv"
to this:
VBA Code:
    myExtension = "*.csv"
and run again.

This should return a message box for each CSV found in that directory.
Does it return any?
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
And what happened?
Are you getting an error on that line, and if so, what exactly does it say?
 
Upvote 0
If I put it this way, with the specific date it will open it right away, but since whenever the file is downloaded the title has the current date, which is why Im trying to get it to work with the Current day function.

Sub OpenCSV()

Workbooks.Open "C:\Users\Bsantana\OneDrive - TTI, Inc\SPACEX\BACKLOGREPORT\OPEN ORDER REPORTS_SPX\SpaceX_Open_Order_Report_10-4-2023.csv"

End Sub


1696435894714.png
 
Upvote 0
Can something be wrong in the current day function? is there another option to write the current day function

& Format(Date, "mm-dd-yyyy") &
 
Upvote 0
Try...

VBA Code:
Format(Date, "mm-d-yyyy")

Hope this helps!
 
Upvote 0
Try...

VBA Code:
Format(Date, "mm-d-yyyy")

Hope this helps!
besides this file there is a second file that needs to open also do I do somehting similar in seperate module or how can I added to this same one?
 
Upvote 0
Try...

VBA Code:
Format(Date, "mm-d-yyyy")

Hope this helps!
Arg! You would not believe how many times I looked at that over and over again, and missed that leading zero on the day!
Thanks for the assist and third set of eyes!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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