Advertisement
Guest User

Untitled

a guest
May 10th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub loopthroughdirectory()
  2.     Dim myFile As String
  3.     Dim myPath As String
  4.     Dim myExtension As String
  5.     Dim myMainFile As String
  6.     Dim mainFileFinalRow As Integer
  7.     Dim fileToCopyFinalRow As Integer
  8.    
  9.     ' Insert the path to the folder in which you have all the excel files
  10.    myPath = "C:\Users\Hendro\Desktop\test\folder_to_loop\"
  11.     myExtension = "*.xls*"
  12.     myFile = Dir(myPath & myExtension)
  13.     MsgBox (myFile)
  14.    
  15.     Do While Len(myFile) > 0
  16.        
  17.         Workbooks.Open (myPath & myFile)
  18.         Workbooks(myFile).Worksheets(1).Activate
  19.        
  20.         fileToCopyFinalRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
  21.         MsgBox (fileToCopyFinalRow)
  22.         ActiveSheet.Range(Cells(2, 1), Cells(fileToCopyFinalRow, 11)).Copy
  23.         Workbooks(myFile).Close
  24.        
  25.         mainFileFinalRow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
  26.         ActiveSheet.Paste Destination:=Worksheets(1).Range(Cells(mainFileFinalRow, 1), Cells(mainFileFinalRow + fileToCopyFinalRow - 1, 11))
  27.        
  28.         myFile = Dir
  29.     Loop
  30. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement