0

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?

3
  • 1
    Do you have those "smart quotes" in your actual code? Otherwise that looks fine. Commented Jun 20, 2022 at 23:12
  • @TimWilliams, ye I have the smart quotes. I figured how to do it, I changed it to ws.Name <> Tasks_Complete.Name Then ws.Delete and it worked for me Commented Jun 20, 2022 at 23:16
  • Just for clarity, Tim is saying that your quote character is different than the default quote character " Commented Jun 20, 2022 at 23:57

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.