VBA help opening text file with 2 dates

rmobley232

New Member
Joined
Jun 30, 2016
Messages
15
I am trying to write a code to import a text file on a daily basis that will have the previous and current yyyymmdd the title of the file. Here is the code when I recorded the macro:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_20190318_20190319.txt", Destination:= _
Range("$A$2"))
.Name = "RI_Maint_20190318_20190319"


Here is how I am attempting to update the code, not having a lot of luck. Thanks so much for the assistance:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_" & (Format(Now(), "yyyymmdd")-1)"_ & " & Format(Now(), "yyyymmdd") & ".txt", Destination:= _
Range("$A$2"))
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try - 365 instead of - 1, better yet, for the previous year use
Code:
Dim yr As Long
yr = Right(Date, 4) - 1
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_" & yr & (Format(Now(), "mmdd"))"_ & " & Format(Now(), "yyyymmdd") & ".txt", Destination:= _
Range("$A$2"))
the - 365 would not work on leap year.
 
Last edited:
Upvote 0
It isn't the year that changes, just the day of the month. So in the example, the date range was Monday, March 18, 2019 to Tuesday, March 19, 2019. The following day would then reference Tuesday, March 19, 2019 through Wednesday, March 20, 2019. Hope this clears it up!
 
Last edited:
Upvote 0
I can get this to work for the previous day:
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_" & Format(Now() - 1, "yyyymmdd") & ".txt"

& this to work for the current day:
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_" & Format(Now(), "yyyymmdd") & ".txt"

I just can not figure out for the life of me how to combine those to get the previous day & current day in the same line. Thanks again!
 
Upvote 0
Something like this:
Code:
Dim prv As String, cur As String
[COLOR=#ff0000]prv[/COLOR] = Format(Date -1, "yyyymmdd")
[COLOR=#ff0000]cur[/COLOR] = Format(Date, "yyyymmdd")
"TEXT;C:\Users\br4rkm\Desktop\RI_Maint_" & [COLOR=#ff0000]prv[/COLOR]" & "_" & [COLOR=#ff0000]cur[/COLOR] & ".txt", Destination:= _
Range("$A$2"))
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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