FryGirl
Well-known Member
- Joined
- Nov 11, 2008
- Messages
- 1,368
- Office Version
- 365
- 2016
- Platform
- Windows
This code works great to combine all workbooks from a folder in another master workbook. Problem though, to get it to run I have to remove the
ActiveWorkbook.Close True part because that closes the workbook that I'm running the code from. The destination workbook.
How can I say, if the file in the loop equals the file that we started with, skip it?
How can I say, if the file in the loop equals the file that we started with, skip it?
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub LoopThroughFolder()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Dim MyFile As String
Dim MyDir As String
Dim Str As String
Dim Wb As Workbook
Dim Rws As Long
Dim Rng As Range
Set Wb = ThisWorkbook
MyDir = ThisWorkbook.Path & Application.PathSeparator
MyFile = Dir(MyDir & "*.xlsm")
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Do While MyFile <> ""
Workbooks.Open (MyFile)
With Sheets("Sheet1")
Rws = .Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range(.Cells(2, 1), .Cells(Rws, 7))
Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
[COLOR=#ff0000]ActiveWorkbook.Close True[/COLOR]
End With
Application.DisplayAlerts = 1
MyFile = Dir()
Loop[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub
[/FONT]