Advertisement
Guest User

hello

a guest
May 9th, 2018
97
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 erow As Integer
  7.    
  8.     ' Insert the name of your main file name
  9.    myMainFile = "Combine.xlsm"
  10.     ' Insert the path to the folder in which you have all the excel files
  11.    myPath = "C:\Users\Hendro\Desktop\test\folder_to_loop\"
  12.     myExtension = "*.xls*"
  13.     myFile = Dir(myPath & myExtension)
  14.    
  15.     Do While Len(myFile) > 0
  16.         If myFile = myMainFile Then
  17.             Exit Sub
  18.         End If
  19.        
  20.         Workbooks.Open (myPath & myFile)
  21.         ' This only copies 1 row of data!
  22.        Range("A2:K2").Copy
  23.         ActiveWorkbook.Close
  24.        
  25.         erow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
  26.         ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 11))
  27.        
  28.         myFile = Dir
  29.     Loop
  30. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement