Please try the next function. It will return independent of the number name pattern. I mean, being "0002345" or "02346", It processes all files from folder and subfolders:
Function getLastFileName(strFold As String, Optional strext As String = "*.*") As String
Dim arrD, i As Long, lastName As String, lngNb As Long, arrN, El
'return all files name in an array
arrD = Filter(Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b /s").StdOut.ReadAll, vbCrLf), "\")
For Each El In arrD 'iterate between the array elements
arrN = Split(El, "\") 'make an array splitting the name by "\"
'check if the name is numeric:
If IsNumeric(Split(arrN(UBound(arrN)), ".")(0)) Then
'compare the lngNb variable (initially 0) with the numeric value:
If lngNb < CLng(Split(arrN(UBound(arrN)), ".")(0)) Then
'addapt lngNb like the bigger number
lngNb = CLng(Split(arrN(UBound(arrN)), ".")(0)): lastName = El
End If
End If
Next
getLastFileName = lastName 'build the necessary path
End Function
It can be tested in the next way:
Sub testGetLastFileName()
Debug.Print getLastFileName("D:\Users\Waleed\Desktop\", "*.xls*")
End Sub
It excludes workbooks name not being a number...
Edited:
The next version returns (and process) only the files in the main folder (excluding files in subfolders):
Function getLastNumberFile(strFold As String, Optional strext As String = "*.*") As String
Dim arrD, i As Long, lastName As String, lngNb As Long, El
'return all files name in an array
arrD = Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b").StdOut.ReadAll, vbCrLf)
If UBound(arrD) = -1 Then MsgBox "Nothing could be found in the path you supplied...": Exit Function
arrD(UBound(arrD)) = "@@##": arrD = Filter(arrD, "@@##", False) 'remove the last (empty) element
For Each El In arrD 'iterate between the array elements
If IsNumeric(Split(El, ".")(0)) Then
'compare the lngNb variable (initially 0) with the numeric value:
If lngNb < CLng(Split(El, ".")(0)) Then
'addapt lngNb like the bigger number
lngNb = CLng(Split(El, ".")(0)): lastName = El
End If
End If
Next
getLastNumberFile = strFold & lastName 'build the necessary path
End Function
It can be used simple setting the necessary workbook as:
Set wb1 = Workbooks.Open(getLastNumberFile("D:\Users\Waleed\Desktop\", "*.xls*"))
And the next version, able to return for both cases. It uses a trick, splitting a joined array using the separator obtained by concatenation between the folder path and "|". Then removing the last array element (in case of returning without subfolders files):
Function getLastFileN(strFold As String, Optional strext As String = "*.*", Optional boolSubfolders = False) As String
Dim arrD, i As Long, lastName As String, lngNb As Long, arrN, El
'return all files name in an array
If boolSubfolders Then 'subfolders included
arrD = Filter(Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b/s").StdOut.ReadAll, vbCrLf), "\")
Else 'without subfolders
arrD = Split(CreateObject("wscript.shell").Exec("cmd /c dir """ & strFold & strext & """ /b").StdOut.ReadAll, vbCrLf)
arrD = Split(strFold & Join(arrD, "|" & strFold), "|") 'add the folder path to the file names
arrD(UBound(arrD)) = "@@##": arrD = Filter(arrD, "@@##", False) 'remove the last (empty) array element
End If
For Each El In arrD 'iterate between the array elements
arrN = Split(El, "\") 'make an array splitting the name by "\"
'check if the name is numeric:
If IsNumeric(Split(arrN(UBound(arrN)), ".")(0)) Then
'compare the lngNb variable (initially 0) with the numeric value:
If lngNb < CLng(Split(arrN(UBound(arrN)), ".")(0)) Then
'addapt lngNb like the bigger number
lngNb = CLng(Split(arrN(UBound(arrN)), ".")(0)): lastName = El
End If
End If
Next
getLastFileN = lastName
End Function