Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hi guys, for the life of me I can't figure out how to add an "On Error Resume Next" line into my working code.
I have a macro that opens files in a directory and if it finds a specific tab name it deletes it, in the event the tab does not exist I want it to close the file and open the next file.
Here is my current code, any help is appreciated. Seems like my code would need to be modified with a IF statement to do this, just not sure how to go about updating.
I have a macro that opens files in a directory and if it finds a specific tab name it deletes it, in the event the tab does not exist I want it to close the file and open the next file.
Here is my current code, any help is appreciated. Seems like my code would need to be modified with a IF statement to do this, just not sure how to go about updating.
Code:
Option Explicit
Public Sub DeleteSheet()
Dim sourceSheet As Worksheet, ws As Worksheet
Dim folder As String, filename As String
Dim destinationWorkbook As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Worksheet in active workbook to be copied as a new sheet to the 160 workbooks
Set sourceSheet = ThisWorkbook.Worksheets("Executive Summary")
'Folder containing workbooks
folder = "\\Wb\acctg\shared\RPTBUD\CASH\TBT\Monthly Forecast\2018\Mar\File Test\Jonathan TBT Test\Terr Files\"
filename = Dir(folder & "*.xls*", False)
Application.AskToUpdateLinks = False 'Supresses External links warning
While Len(filename) <> 0
Debug.Print folder & filename
Set destinationWorkbook = Workbooks.Open(folder & filename)
destinationWorkbook.Activate
destinationWorkbook.Sheets("Executive Summary").Delete
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Wend
End Sub