VBA Open .txt file with a string

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a VBA code that works like a charm that displays a dialog box and allows a user to select a file. I am trying to enhance it to force the file to be opened based off a set criteria. For example, the file format is AAA_20200529_0513. So a file will be saved daily with AAA_ then the date in YYYYMMDD then _ and time in HHMM. I really only care about the date it was saved and the file name so. AAAA_YYYYMMDD is met. So anyway to force the file that is opened to meet that criteria, and if it doesnt exist to kick out an error message?

Sub OpenCopyTXT()
Dim Fname As String
With Application.FileDialog(3)
.InitialFileName = "C:\Windows"
.Filters.Add "Text Files Only", "*.txt"
If .Show = -1 Then Fname = .SelectedItems(1)
End With
Workbooks.OpenText Fname
End Sub
 
Ok I see exactly what it is doing and that works great. I see where the misinterpretation was. When I said force the file to be opened what i really meant was remove the dialogbox and the VBA will automatically open the file based off the criteria.
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I have a VBA code that works like a charm that displays a dialog box and allows a user to select a file. I am trying to enhance it to force the file to be opened based off a set criteria.
Imo my code meets this.
 
Upvote 0
When I said force the file to be opened what i really meant was remove the dialogbox and the VBA will automatically open the file based off the criteria.
Which file exactly? There could be more files meeting your criteria (ie AAA_yyyymmdd_xxxx.txt)
 
Upvote 0
Which file exactly? There could be more files meeting your criteria (ie AAA_yyyymmdd_xxxx.txt)
Right so that variable is YYYYMMDD needs to equal today's date each day will have 1 file and on that day I run it i only want today's file.
 
Upvote 0
I see, that's a different story. Try this:
VBA Code:
Sub OpenCopyTXT_r2()
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFullName   As String
    
    sPath = "C:\Users\ItalianPlatinum\Documents\"      ' <<<<< change accordingly
        
    sPartial = "AAA_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFullName = Dir(sPath & sPartial)
    If Len(sFullName) > 0 Then
        Workbooks.OpenText sFullName
    Else
        MsgBox "File not found.", vbExclamation
    End If
End Sub
 
Upvote 0
Works like an absolute CHARM! Thank you so much! And yes i figured my wording was not very clear so apologies on that. :)
 
Upvote 0
The last code from GWTEB works. each day the file will change by todays date. and only 1 file will be opened per day.
That p;ost was referring to my comments. I couldn't delete the post, so I just put the statement in to indicate that the post should be ignored.
 
Upvote 0
Works like an absolute CHARM! Thank you so much! And yes i figured my wording was not very clear so apologies on that. :)
Glad to help.
Being a person who still struggles every day to express himself clearly, I will be the last one to blame you.
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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