I have created this macro to clear all data from tables on 7 different worksheet tabs; however, the macro aborts if one of the worksheets happens to have been cleared already. Is there a way to avoid this, possibly with an if statement to check if the sheet is empty already (other than header row of course) and if so skip to the next sheet?
Also, I put an onerror goto in the code, with the intent that something went wrong with the macro, that it would skip down to the end and give the error message (rather than the runtime msg with end, abort, etc, which would confuse others when using the macro); however the error msg shows each time, even if the macro was successful? Am I misusing it?
Below is what I have for my code already if someone could take a look at it to make suggestions. Also, if there's anything that could be revised to be more efficient, etc, please advise. I am still very new and learning VBA....Thanks!
Sub Clear_Worksheets() ' ' Clear all worksheets to prepare for importing new data On Error GoTo Errormessage: If MsgBox("You are about to clear all data from each tab. Do you want to continue?", vbYesNo + vbExclamation) = vbNo Then Exit Sub Else Sheets("Inventory Status").Visible = True Sheets("Inventory Status").Unprotect Password:=“password” Sheets("Inventory Status").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Columns("A:AF").Hidden = False Worksheets("Inventory Status").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Columns("L").Hidden = True Columns("P:R").Hidden = True Columns("Y:AA").Hidden = True Range("AC2").Select ActiveCell.FormulaR1C1 = _ "=IFERROR(IF(ISNA(VLOOKUP(RC[-26],Wkly_Notes,3,0)),VLOOKUP(RC[-26],Eviction,6,0),VLOOKUP(RC[-26],Wkly_Notes,3,0)),"""")" Range("A2").Select Sheets("Compliance").Visible = True Sheets("Compliance").Unprotect Password:=“password” Sheets("Compliance").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Compliance").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select Sheets("Title").Visible = True Sheets("Title").Unprotect Password:=“password” Sheets("Title").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Title").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select Sheets("Eviction").Visible = True Sheets("Eviction").Unprotect Password:=“password” Sheets("Eviction").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Eviction").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select Sheets("Additional Data").Visible = True Sheets("Additional Data").Unprotect Password:=“password” Sheets("Additional Data").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Additional Data").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select Sheets("Weekly Notes").Visible = True Sheets("Weekly Notes").Unprotect Password:=“password” Sheets("Weekly Notes").Select 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Weekly Notes").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select Sheets("Closed").Visible = True Sheets("Closed").Select Sheets("Closed").Unprotect Password:=“password” 'ActiveSheet.Unprotect 'ActiveSheet.ShowAllData Worksheets("Closed").AutoFilterMode = False Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp Range("A2").Select End If Errormessage: MsgBox "Macro aborted. Check Worksheet...has sheet been cleared already?" End Sub