Announcement

Collapse
No announcement yet.

Unconfigured Ad Widget

Collapse

Unhide Specific Worksheets Macro

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unhide Specific Worksheets Macro

    I'm trying to unhide several sheets (then hide them again at the end of the macro using = False). When I run this macro nothing happens. What's wrong with the code?

    Code:
    For Each ws In Worksheets
        If ws.Name = ("OP KPI 1 pivot") And ws.Name = ("OP KPI 2 pivot") Then
        With ws
            .Visible = True
        End With
            End If
        Next ws

  • #2
    Re: Unhide Specific Worksheets Macro

    It's probably this line of code

    Code:
    If ws. Name = ("OP KPI 1  pivot") And ws.Name = ("OP KPI 2 pivot") Then
    Change the AND to OR

    Code:
    If ws. Name = ("OP KPI 1  pivot") Or ws.Name = ("OP KPI 2 pivot") Then
    HTH

    Craig

    Comment


    • #3
      Re: Unhide Specific Worksheets Macro

      Code:
      Sub Show_Hide_Sheets()
          Dim ws As Worksheet
          
          For Each ws In Worksheets
              If ws.Name = "Sheet1" Or ws.Name = "Sheet2" Then
                  ws.Visible = Not ws.Visible
              End If
          Next ws
      End Sub
      AAE
      ----------------------------------------------------

      Forum Rules | Message to Cross Posters | How to use Tags

      Comment


      • #4
        Re: Unhide Specific Worksheets Macro

        Thanks for getting back to me so quickly. But if I try with all the sheets, I get a syntax error, and I can't figure out what's wrong:

        Code:
        For Each ws In Worksheets
            If ws.Name = ("OP KPI 1 pivot") Or ws.Name = ("OP KPI 2 pivot") Or ws.Name = ("OP KPI 3 pivot") Or ws.Name = ("OP KPI 4 pivot"), _
            Or ws.Name = ("OP KPI 5 pivot") Or ws.Name = ("SC KPI 1 pivot") Or ws.Name = ("SC KPI 2 pivot") Or ws.Name = ("SC KPI 3 pivot"), _
            Or ws.Name = ("SC KPI 4 pivot") Then
            With ws
                .Visible = True
            End With
                End If
            Next ws

        Comment


        • #5
          Re: Unhide Specific Worksheets Macro

          A Select Case Statement would the best.

          Why not just show ALL sheets and hide the ones to remain remain hidden, if any?

          Or, use something like;
          Code:
          ws.Visible=ws.Name Like "*pivot"
          Your code fails due the commas before the line continuation characters.

          Comment


          • #6
            Re: Unhide Specific Worksheets Macro

            You got a point, Dave. I'll try that. Thanks.

            Comment

            Trending

            Collapse

            There are no results that meet this criteria.

            Working...
            X