Advertisement
xlujiax

Abrir excel de subcarpetas

Jan 29th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Public Sub AbrirLibros()
  4.  
  5.     Dim fso As Object, fCarpeta As Object, tmpCarpeta As Object
  6.  
  7.     Dim Fichero As Object, tmpFichero As Object
  8.  
  9.     Dim strRutaInicial As String
  10.  
  11.  
  12.     strRutaInicial = "D:\Check_List_Hold_Point"
  13.  
  14.  
  15.     Set fso = CreateObject("Scripting.FileSystemObject")
  16.  
  17.     Set fCarpeta = fso.GetFolder(strRutaInicial)
  18.  
  19.  
  20.     For Each tmpFichero In fCarpeta.Files
  21.  
  22.     If LCase(Mid(tmpFichero.Name, InStr(tmpFichero.Name, ".") + 1)) = "xlsx" Then
  23.         Workbooks.Open tmpFichero.Path
  24.         ActiveWorkbook.Close savechanges:=False
  25.     End If
  26.  
  27.     Next tmpFichero
  28.  
  29.  
  30.     Recursivo strRutaInicial
  31.  
  32. End Sub
  33.  
  34.  
  35. Private Sub Recursivo(ByVal RutaInicial As String)
  36.  
  37.     Dim fso As Object, fCarpeta As Object, tmpCarpeta As Object
  38.  
  39.     Dim Fichero As Object, tmpFichero As Object
  40.  
  41.  
  42.     Set fso = CreateObject("Scripting.FileSystemObject")
  43.  
  44.     Set fCarpeta = fso.GetFolder(RutaInicial)
  45.  
  46.  
  47.     For Each tmpCarpeta In fCarpeta.SubFolders
  48.  
  49.         For Each tmpFichero In tmpCarpeta.Files
  50.  
  51.             If LCase(Mid(tmpFichero.Name, InStr(tmpFichero.Name, ".") + 1)) = "xlsx" Then
  52.                 Workbooks.Open tmpFichero.Path
  53.                 ActiveWorkbook.Close savechanges:=False
  54.             End If
  55.  
  56.         Next
  57.  
  58.         Recursivo tmpCarpeta.Path
  59.  
  60.     Next tmpCarpeta
  61.  
  62. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement