Option Explicit
Sub MiRuta()
Dim rUta As String
'Define here your main Path (Ruta)
rUta = Sheets("Instructions").Range("B1")
Call Mostrar_Archivos(rUta)
End Sub
Sub Mostrar_Archivos(rUta)
'Sección 1: Declare Variables and Objects
Dim fS As Object, cArpeta As Object, aRchivo As Object, sUbcarpeta As Object
Dim mIarchivo As String
Set fS = CreateObject("Scripting.FileSystemObject")
'Sección 2: Chek proper rUta
If rUta = "" Then
Exit Sub
ElseIf Right(rUta, 1) <> "\" Then
rUta = rUta & "\"
End If
'Sección 3: Object Folder from rUta
On Error GoTo ErrHandler
Set cArpeta = fS.GetFolder(rUta)
'Sección 4: Retrieve files from object Folder
For Each aRchivo In cArpeta.Files
mIarchivo = rUta & aRchivo.Name
ActiveCell.Value = mIarchivo 'List all files
ActiveCell.Offset(1, 0).Select
'***
'Here to call your sub MoveData()
' I sugest you to pass the File name variable: mIarchivo
'MoveData (mIarchivo)
Call MoveData(mIarchivo)
'***
Next
'Sección 5: Search for subFolders in object Folder
For Each sUbcarpeta In cArpeta.SubFolders
Mostrar_Archivos (sUbcarpeta)
Next
'Sección 6: Auto-ajustar columnas y salir
ActiveCell.EntireColumn.AutoFit
Exit Sub
ErrHandler:
ActiveCell.Value = "Ruta inexistente"
End Sub
Sub MoveData()
Application.EnableCancelKey = xlDisabled
Dim MyPath As String
Dim FilesInPath As String
Dim Filename As String
Dim wb As Workbook
Dim rng As Range
Dim rngCell As Range
Dim rngTarget As Range
Dim calcmode As Long
Dim lCount As Long
Dim lX As Long
Dim LR As Long
' Set various application properties.
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
.StatusBar = "Importing data..."
End With
With ActiveSheet
If MsgBox("WARNING!" & vbCrLf & "If you proceed, all data in this worksheet will be overwritten." & vbCrLf & _
"Do you wish to continue?", vbYesNo) = vbYes Then
On Error Resume Next
ActiveSheet.Range("A2").Select
ActiveSheet.ShowAllData
ActiveSheet.UsedRange.Offset(1).EntireRow.ClearContents
ElseIf MsgBox("Import cancelled.", vbOKOnly) = vbOK Then
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = calcmode
End With
Exit Sub
End If
End With
' Change this to the path\folder location of your files.
'MyPath = Sheets("Instructions").Range("B1")
' Add a / to the end of file path, if not present
' If Right(MyPath, 1) <> "\" Then
' MyPath = MyPath & "\"
' End If
'Filename = Dir(MyPath & "*.xls")
' If there are no Excel files in the folder, exit.
'FilesInPath = Dir(MyPath & "*.*")
'If FilesInPath = "" Then
' MsgBox "No files found"
' Exit Sub
'End If
'Do While Filename <> ""
Set rngTarget = Worksheets("Master").Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1)
Set wb = Workbooks.Open(MyPath & Filename)
Set rng = wb.Sheets("Sheet1").Range("c4,c5,c15,c16,c17,c25,c26,c27,d15,d16,d17,d25,d26,d27,e15,e16,e17,e25,e26,e27")
For Each rngCell In rng
lX = lX + 1
rngTarget.Offset(0, lX - 1).Value = rngCell.Value 'For horzontal output
'rngTarget.Offset(lX - 1, 0).Value = rngCell.Value 'For vertical output
Next
Workbooks(Filename).Close True
Filename = Dir
lX = 0
'Loop
'once import complete, sort by date, enter target value, filter to exclude blanks
LR = ActiveSheet.UsedRange.Rows.Count
Range("V2").Select
ActiveCell.FormulaR1C1 = "=RC[-20]"
Range("V3").Select
ActiveCell.FormulaR1C1 = "=IF(R[-1]C[-21]=RC[-21],"""",RC[-21])"
Range("V3").AutoFill Destination:=Range("V3:V" & LR)
Range("W2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-21]="""",22,RC[-21])"
Range("W2").AutoFill Destination:=Range("W2:W" & LR)
ActiveSheet.Range("U2").Value = Sheets("Instructions").Range("B3").Value
Range("U2").AutoFill Destination:=Range("U2:U" & LR)
Range("A2").CurrentRegion.Sort key1:=Range("A2"), order1:=xlAscending, Header:=xlYes
ActiveSheet.ListObjects("chtData").Resize Range("$A$1:$W$1000")
ActiveSheet.ListObjects("chtData").Range.AutoFilter Field:=1, Criteria1:="<>"
Range("C:E").Interior.ColorIndex = 15
Range("I:K").Interior.ColorIndex = 15
Range("O:Q").Interior.ColorIndex = 15
Columns("C:C").Select
Selection.NumberFormat = "General"
Columns("V:V").Select
Selection.NumberFormat = "dd/mm/yyyy"
Columns("W:W").Select
Selection.NumberFormat = "[$-F400]h:mm:ss am/pm"
Range("A2").Select
'Worksheets("chtShiftData").Visible = False
MsgBox "The import is complete, Click OK to continue"
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = calcmode
.StatusBar = False
End With
Sheets("chtShiftData").Select
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("$A$1:$D$1000").AutoFilter Field:=1, Criteria1:="<>"
Sheets("Master").Activate
End Sub
Bookmarks