Modify existing VBA to accommodate opening most recent file

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
880
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I am looking for a way to open a file that is the most recent. I have code to open the file if it is a partial file but I have a new issue where there are two versions that were saved and just want the most recent one opened. Is someone able to assist? I would like to use the below still cause it is embedded all over my macro :)

VBA Code:
Function OpenCopyST() As Boolean
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String
   
    sPath = "\\xxxxxx\xxxx$\xxxx\xxxx\xxx xxxx\"      ' <<<<< change accordingly
   
    sPartial = "vi_j_dt_dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 2)), TrailingMinusNumbers:=True
        OpenCopyST = True
    Else
        OpenCopyST = False
    End If
End Function
 
Would recommend to take a piece of paper and a pencil and write down (for yourself, in pseudo code) all the possible "IF this .... and wanted THEN do that" scenarios in the smallest possible form, so only one situation on the IF and one action on the THEN. If your list is completed then try to merge the similar IFs and the similar THENs and try to determine the required program flow based on that.
The IF ..THEN ..ELSE construct is a common one for branching and I would recommended to practice with.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
ahhhhh i had a mixup i changed the wrong end if to else and after i converted the one i wanted its working as i would expect. Now i can see what you mean. Teach a man to fish :)

On the OpencopyST function you created I have a second wild card I had to create a new post for if you are bored ;)

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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