The following code gets stuck in a continuous loop opening and closing a file that does not satisfy the criteria of the If statement.
I hoped that by placing the Else statement just before Loop that it would continue the Loop and move down the Hotel List to the next file path. It reopens and closes the first file that doesn't satisfy the criteria of the If. Which in my instance is the third file on the list.
For those files not satisfying the If statement I want to close them and move on to the next. Apart from this issue, the code does as expected.
All of the code:
Sub Control_AR_AD_Copy_Hotel_DataArchive_And_Control()
'
' In summary loops through list of hotel file paths and copies the data archive and control information until complete.
'
' Creates list of hotels we expect an aged debtor report for.
'
Sheets("Hotel_List").Select
Range("A4:Z103").Select
Selection.ClearContents
Range("A1").Select
Sheets("Tables").Select
ActiveSheet.Range("$D$4:$Q$103").AutoFilter Field:=4, Criteria1:="Yes"
Range("D4:Q103").Select
Selection.Copy
Sheets("Hotel_List").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Range("A1").Select
' Copy Data Archive to Previous and clear data archive
Sheets("Data_Archive_All").Select
Range("A2:Z2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("A2").Select
Sheets("Data_Archive_Previous").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A2").Select
Application.CutCopyMode = False
Sheets("Data_Archive_All").Select
Range("A2:Z2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
Sheets("Control").Select
Range("A1").Select
' Select cell where Loop begins as first line of data.
Sheets("Hotel_List").Select
Range("F4").Select
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
' Open hotel file as read only.
Dim my_wb As Workbook
Dim file_path As String
file_path = ActiveCell
Set my_wb = Workbooks.Open(Filename:=file_path, ReadOnly:=True)
' Activate Workbook
Dim hotel_wb As Workbook
Set hotel_wb = ActiveWorkbook
hotel_wb.Activate
' Set If Statement based upon values in Data_Archive and Control worrksheet
If Sheets("Data_Archive").Range("G2") > "0" And Sheets("Control").Range("T18") = "Yes" Then
' Remove filter from Hotel's Data Archive and unprotect worksheet.
Sheets("Data_Archive").Select
Range("A2").Select
ActiveSheet.Unprotect "DebRepAcc"
ActiveSheet.AutoFilter.ShowAllData
' Apply filter and copy data from Hotel's Data Archive.
Range("A2").Select
ActiveSheet.Range("B1").AutoFilter Field:=25, Criteria1:="1"
Range("$A$2:$Z$50001").Select
Selection.Copy
' Paste data into Coonsolidated Data Archive in next blank cell.
Workbooks("Consolidated_Accounts_Receivable_Aged_Debtors").Activate
Sheets("Data_Archive_All").Select
Range("A:A").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
' Remove filter from Hotel's Data Archive and protect worksheet.
hotel_wb.Activate
Sheets("Data_Archive").Select
ActiveSheet.AutoFilter.ShowAllData
ActiveSheet.Protect "DebRepAcc", DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFiltering:=True
' Copy data from Control worksheet.
Sheets("Control").Select
ActiveSheet.Calculate
Range("C3:T18").Select
Selection.Copy
' Paste Control Data into Control Convert worksheet.
Workbooks("Consolidated_Accounts_Receivable_Aged_Debtors").Activate
Sheets("Control_Convert").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveSheet.Calculate
' Copy data from Control_Convert into Control_Current in next blank cell.
Sheets("Control_Convert").Select
Range("A20:O20").Select
Selection.Copy
Sheets("Control_Current").Select
Range("A:A").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
' Close hotel's workbook without saving changes.
hotel_wb.Activate
ActiveWorkbook.Close savechanges:=False
' Step down 1 row from present location.
Workbooks("Consolidated_Accounts_Receivable_Aged_Debtors").Activate
Sheets("Hotel_List").Select
ActiveCell.Offset(1, 0).Select
' End If Function
Else
ActiveWorkbook.Close savechanges:=False
End If
Loop
'
' Copy data from Control_Current into Control_Archive in next blank cell.
'
Workbooks("Consolidated_Accounts_Receivable_Aged_Debtors").Activate
Sheets("Control_Current").Select
Range("A2:O101").Select
Selection.Copy
Sheets("Control_Archive").Select
Range("A:A").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Sheets("Control").Select
Range("A1").Select
End Sub