0726
0726

Reputation: 21

VbA Delete sheets

I am trying to delete all the sheets which do not equal specific code names but instead of doing that, my code is deleting the sheets that I want to keep.the coding is below:

For each ws In wb.worksheets

If ws.CodeName <> “Tasks_Complete” And ws.CodeName <> “Issues_Tasks” Then
ws.Delete
End If

Next ws 

Instead it deletes the sheets with the code names and keep the sheet that I do not want. Am I missing something from the code?

Upvotes: 0

Views: 620

Answers (1)

Tim Williams
Tim Williams

Reputation: 166136

You have "smart quotes" in your posted code - those are not the same as regular double-quotes.

Select Case is maybe easier to read:

For each ws In wb.worksheets
    Select case ws.CodeName
        Case "Tasks_Complete", "Issues_Tasks"  'do nothing
        Case Else: ws.Delete
    End select
Next ws

Upvotes: 1

Related Questions