Hello, I'm very new to VBA and I'm trying to amend the below program. This code deletes all worksheets with with "Text". All I need to change is delete all worksheets without "Text". Any help on this would be appreciated.
Dim i As Integer, n As Integer
n = ThisWorkbook.Worksheets.Count
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For i = n to 1 step -1
On Error Resume Next
If InStr(1, Sheets(i).Name, "Text") Then Sheets(i).Delete
On Error GoTo 0
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
The worksheets in my model are called Page 1, Page 2, Page 3 ext. The text I am searching for is "CAPITAL PROJECTS". The macro is saved in a different workbook than the macro is being run in called CapitalProjectsAnalysis.xlsx. (Because of a silly company policy I am not allowed to save macro-enabled workbooks) When running the macro, I leave CapitalProjectsAnalysis.xlsx open. Here is what I have tried so far:
Sub CapitalProjects()
Dim i As Integer, n As Integer
n = ThisWorkbook.Worksheets.Count
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For i = n To 1 Step -1
On Error Resume Next
If Not InStr(1, Page(i).Name, "CAPITAL PROJECTS") Then Page(i).Delete
On Error GoTo 0
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I'm not sure if this code will even work because when I run the macro it returns Compile Error: Sub or Function not defined. I hope this means I'm missing a very simple step.
Bookmarks