0

Using the code below where I'm able to select all sheets that contains the word "ba" on it however, if those sheets/tabs are hidden, I can't, I get a "Run-time errror '1004':" warning.

Any suggestion how could I make this code work with hidden sheet/tabs? So it shows all sheets/tabs names with "ba" even if it's hidden shows up? If they are hidden, I want them to appear or .Visible = True

Sub listray()
Dim ws As Worksheet, flg As Boolean
For Each ws In Sheets
If LCase(ws.Name) Like "*ba*" Then
    ws.Select Not flg
    flg = True
End If
Next
End Sub
End Sub
3
  • Why do you need to Select the sheet? Commented Sep 20, 2018 at 20:44
  • By select, I been unhide sheets that contain certain text in the name. Commented Sep 20, 2018 at 21:06
  • You don't need to Select a sheet to hide/unhide it. Commented Sep 20, 2018 at 21:10

2 Answers 2

3

The sheets have to be visible to be selected.

If LCase(ws.Name) Like "*ba*" Then
    ws.Visible = xlSheetVisible
    ws.Select Not flg
    flg = True
End If
Sign up to request clarification or add additional context in comments.

Comments

2

You do not need to Select a worksheet to hide/unhide it. This will cycle through sheets and only leave sheets visible that have name Like "*ba*"

Sub listray()

Dim ws As Worksheet

Application.ScreenUpdating = False
    For Each ws In Sheets
        ws.Visible = LCase(ws.Name) Like "*ba*"
    Next
Application.ScreenUpdating = True

End Sub

2 Comments

This is also valid if all that needs to be done is the unhiding. It's unclear if there are followup actions after the sheets are selected though and the OP's reply to your question didn't really make it any less of an unknown. I'm giving this answer an upvote based on the comment reply that said the select was just to perform the unhide.
Yea that's what i'm basing this answer on. If it doesn't fit OPs need, and the Boolean flag is used for other things, than maybe this will do for future readers :p

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.