Advertisement
Guest User

hahahaha

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.    
  14.     Do While Len(myFile) > 0
  15.        
  16.         Workbooks.Open (myPath & myFile)
  17.        
  18.         fileToCopyFinalRow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
  19.         Range(Cells(2, 1), Cells(fileToCopyFinalRow, 11)).Copy
  20.         ActiveWorkbook.Close
  21.        
  22.         mainFileFinalRow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
  23.         ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(mainFileFinalRow, 1), Cells(mainFileFinalRow + fileToCopyFinalRow - 2, 11))
  24.        
  25.         myFile = Dir
  26.     Loop
  27. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement