Macro para abrir archivo IF

Status
Not open for further replies.

ZTK

Board Regular
Joined
Aug 20, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Buen dia.

Acudo a ustedes ya que aqui he aprendido bastantes detalles de VBA.

tengo una macro que me funciona perfecto, gracias a un foro de esta WEB pude armarla conforme a mis necesidades.

Su función es abrir un archivo de una carpeta, generalmente los archivos tienen nombres genéricos, por ejemplo "SUCURSAL _XXX_AGOSTO" el unco valor que cambia es el mes.

Lo que me gustaría es que si no encuentra el archivo correspondiente al mes actual abra el del mes anterior (en base al ejemplo seria el del mes de Julio), ya sea mediante un MSBOX de confirmacion/aviso o directamente abrirlo.

Les adjunto la MACRO que tengo:


Dim xfolders As New Collection

Sub Buscar_Fichas_de_Datos()
Dim sPath As String
Dim xfold As Variant, arch As Variant
Dim bexist As Boolean
Dim sPartialName As String
Dim Mensaje As String

Set xfolders = Nothing
sPath = "C:\Users\Miguel Angel\Desktop\MIGUEL\FORMATOS pruebas\FICHAS pruebas"
xfolders.Add sPath 'CORRIGE DIRECTORIO
Mensaje = "¿Qué sucursal buscas?"
sPartialName = InputBox(Mensaje, "Centro de Control")
Call AddSubDir(sPath)
For Each xfold In xfolders
arch = Dir(xfold & "\" & "Sucursal" & " " & sPartialName & " " & Application.WorksheetFunction.Proper(MonthName(VBA.Month(Date), False)) & ".*")
If arch <> "" Then Exit For
Next
If arch = "" Then

MsgBox "No se encontró el archivo"
Else
ActiveWorkbook.FollowHyperlink xfold & "\" & arch
End If
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





DE ANTEMANO GRACIAS
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Duplicate to: Open file IF

Welcome to the MrExcel board!

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. I have left the one in Excel questions as you should get more potential helpers in that English language forum.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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