Good day; I ask for your help, I already have a macro that helps me open a file from a specific path, the detail is that every month the name of the folder changes, I would like to see the possibility of leaving as a "general" path, let's say, the desktop of my PC and from there to look for the file in some other of the subfolders.
As an example, this month the folder where the file is located is possibly called "January 01", the following month it could be "Logs" and so on; Inside these folders there are files with 3-digit numbers + the name of the month.
The Macro that I have already does the function of opening the file if I only put the name of the first 3 digits, but as I mentioned, I would like to "expand" the search range from the desktop and from there to the next subfolder.
I don't know if I understand myself.
The code I have is the following:
Sub Buscar_Fichas_de_Datos()
Dim sPath As String, sName As String, sMonth As String
Dim xfold As Variant, arch As Variant
Dim bexist As Boolean
Dim sPartialName As String
Dim Mensaje As String
Dim m As Long, n As Long
Set xfolders = Nothing
sPath = "C:\Users\XXXXX\Desktop\Datos Diciembre"
xfolders.Add sPath
Mensaje = "¿Qué Ficha buscas?" & vbNewLine & vbNewLine & _
"Escribe el número a 3 dígitos"
sPartialName = InputBox(Mensaje, "Pruebal")
sName = sPartialName & " "
m = VBA.Month(Date)
Call AddSubDir(sPath)
Do While True
sMonth = Application.WorksheetFunction.Proper(MonthName(m, False))
For Each xfold In xfolders
arch = Dir(xfold & "\" & sName & sMonth & ".*")
If arch <> "" Then Exit For
Next
If arch = "" Then
If MsgBox("¡¡¡La ficha NO ESTA ACTUALIZADA!!!" & vbNewLine & vbNewLine & _
"¿Abrir el mes anterior?", vbQuestion + vbYesNo, "Abrir Archivo") = vbNo Then
Exit Do
Else
n = n + 1
m = Month(DateSerial(Year(Date), Month(Date) - n, 1))
End If
Else
ActiveWorkbook.FollowHyperlink xfold & "\" & arch
Exit Do
End If
Loop
End Sub
'
Sub AddSubDir(lPath As Variant)
Dim SubDir As New Collection, DirFile As Variant, sd As Variant
If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
DirFile = Dir(lPath & "*", vbDirectory)
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If ((GetAttr(lPath & DirFile) And vbDirectory) = 16) Then
SubDir.Add lPath & DirFile
End If
End If
DirFile = Dir
Loop
For Each sd In SubDir
xfolders.Add sd
Call AddSubDir(sd)
Next
End Sub
I thank you in advance for your attention and support.
As an example, this month the folder where the file is located is possibly called "January 01", the following month it could be "Logs" and so on; Inside these folders there are files with 3-digit numbers + the name of the month.
The Macro that I have already does the function of opening the file if I only put the name of the first 3 digits, but as I mentioned, I would like to "expand" the search range from the desktop and from there to the next subfolder.
I don't know if I understand myself.
The code I have is the following:
Sub Buscar_Fichas_de_Datos()
Dim sPath As String, sName As String, sMonth As String
Dim xfold As Variant, arch As Variant
Dim bexist As Boolean
Dim sPartialName As String
Dim Mensaje As String
Dim m As Long, n As Long
Set xfolders = Nothing
sPath = "C:\Users\XXXXX\Desktop\Datos Diciembre"
xfolders.Add sPath
Mensaje = "¿Qué Ficha buscas?" & vbNewLine & vbNewLine & _
"Escribe el número a 3 dígitos"
sPartialName = InputBox(Mensaje, "Pruebal")
sName = sPartialName & " "
m = VBA.Month(Date)
Call AddSubDir(sPath)
Do While True
sMonth = Application.WorksheetFunction.Proper(MonthName(m, False))
For Each xfold In xfolders
arch = Dir(xfold & "\" & sName & sMonth & ".*")
If arch <> "" Then Exit For
Next
If arch = "" Then
If MsgBox("¡¡¡La ficha NO ESTA ACTUALIZADA!!!" & vbNewLine & vbNewLine & _
"¿Abrir el mes anterior?", vbQuestion + vbYesNo, "Abrir Archivo") = vbNo Then
Exit Do
Else
n = n + 1
m = Month(DateSerial(Year(Date), Month(Date) - n, 1))
End If
Else
ActiveWorkbook.FollowHyperlink xfold & "\" & arch
Exit Do
End If
Loop
End Sub
'
Sub AddSubDir(lPath As Variant)
Dim SubDir As New Collection, DirFile As Variant, sd As Variant
If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
DirFile = Dir(lPath & "*", vbDirectory)
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If ((GetAttr(lPath & DirFile) And vbDirectory) = 16) Then
SubDir.Add lPath & DirFile
End If
End If
DirFile = Dir
Loop
For Each sd In SubDir
xfolders.Add sd
Call AddSubDir(sd)
Next
End Sub
I thank you in advance for your attention and support.