ItalianPlatinum
Well-known Member
- Joined
- Mar 23, 2017
- Messages
- 880
- Office Version
- 365
- 2019
- Platform
- Windows
Hello - I have current VBA that a user helped me in a prior thread here. It will open a file based off partial parameters then open the most recent file. I have one curve ball now. I need to open the file for the prior business day as well (factoring holidays). The complexity of doing that has me little worried of how to approach. Would someone kindly assist?
VBA Code:
Function OpenCopyST() As Workbook
Dim sPath As String
Dim sPartial As String
Dim sFName As String
sPath = "\\XXXXXXXXXXXXXXXXXXXXXXXd\" ' <<<<< change accordingly
sPartial = "v_j_dist_periodic_" & 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)
Dim arr() As Variant, FullName As String, i As Long
Do While Len(sFName) > 0
ReDim Preserve arr(i)
arr(i) = sPath & sFName
i = i + 1
sFName = Dir
Loop
If i > 0 Then
FullName = GetMostRecentFileFromArray(arr)
Dim ErrNum As Long
On Error Resume Next
Workbooks.OpenText FullName, 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
ErrNum = VBA.Err.Number
On Error GoTo 0
If ErrNum = 0 Then
Set OpenCopyST = ActiveWorkbook
End If
End If
End Function
Public Function GetMostRecentFileFromArray(ByRef argArr() As Variant) As String
Dim fso As Object, i As Long, arrEntry As Long, oFile As Object, MostRecentFileDate As Double
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
For i = LBound(argArr) To UBound(argArr)
Set oFile = fso.GetFile(argArr(i))
If oFile.DateLastModified > MostRecentFileDate Then
MostRecentFileDate = oFile.DateLastModified
arrEntry = i
End If
Next i
GetMostRecentFileFromArray = argArr(arrEntry)
End Function