Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hi guys, working on a code that I got to work but need a small addition to this code.
Currently, the code will loop thru a named range of file names and then opens and deletes specific tabs and then closes and loops to the next file. I need to add a piece of code that will move on if it comes across a filename that is not in the folder and if the last file in the list of file names doesn't exist then to be completed with the loop.
Any help is appreciated. Almost there with this project!
Currently, the code will loop thru a named range of file names and then opens and deletes specific tabs and then closes and loops to the next file. I need to add a piece of code that will move on if it comes across a filename that is not in the folder and if the last file in the list of file names doesn't exist then to be completed with the loop.
Any help is appreciated. Almost there with this project!
Code:
Option Explicit
'--------------------------------------------------------
'--- Deletes the two new sheets
'---------------------------------------------------------
Public Sub DeleteSheet()
Dim SourceSheet As Worksheet, SourceSheet2 As Worksheet
Dim folder As String, filename As String, vFilename As String, vFileWKBK, vfilepath As String
Dim vCell As Range
Dim DestBook As Workbook, destinationWorkbook As Workbook
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
For Each vCell In Range("LISTTERRITORYNAME").Cells
vfilepath = ThisWorkbook.Path & "\" 'Uses the files folder location for directory
vFileWKBK = Range("TBTPREFIX").Value & " - " & vCell.Value & ".xls*"
vFilename = vfilepath & vFileWKBK
Application.AskToUpdateLinks = False 'Supresses External links warning
Set destinationWorkbook = Workbooks.Open(vFilename)
destinationWorkbook.Activate
On Error Resume Next
destinationWorkbook.Sheets("R&O").Delete
destinationWorkbook.Sheets("Executive Summary").Delete
destinationWorkbook.Sheets("Sheet1").Delete
destinationWorkbook.Sheets("Sheet2").Delete
On Error GoTo 0
destinationWorkbook.Close True
Next
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
MsgBox "Sheets have been removed"
End Sub