A macro to show and hide specific rows in specific worksheets

K0st4din

Well-known Member
Joined
Feb 8, 2012
Messages
501
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
thanks to the forum, I found two solutions for my case, but I can't put together and make a macro that does what I want.

The first macro from the forum is this one

Code:
Private Sub Workbook_Open()
    HideUnhide True
End Sub
Code:
Public Sub HideUnhide(Optional LockSheet As Boolean)
'LockSheet = true forces sheet(s) to be locked. Use on workbook open.
'LockSheet not specified or false; sheet(s) locked based on button caption.

    Dim shp As Object: Set shp = Sheets("Product").Shapes("Button 1").DrawingObject
    If Not LockSheet And shp.Caption = "Unhide" Then
        If Unhide(InputBox("Enter password"), "London", "NY", "Paris") Then
            shp.Caption = "Hide"
        End If
    Else
        If Hide("Plovdiv", "London", "NY", "Paris") Then
            shp.Caption = "Unhide"
        End If
    End If
End Sub
 
Private Function Hide(pw As String, ParamArray sheetname() As Variant) As Boolean
On Error Resume Next
    For Each sht In sheetname
        With Sheets(sht)
            Select Case Err.Number
            Case 0
                .Range( _
                    "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38," & _
                    "45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
                    ).EntireRow.hidden = True
                
                Select Case Err.Number
                Case 0, 1004 'already hidden?
                Case Else: Debug.Print Err.Number, Err.Description
                End Select
                Err.Clear
                
                .Protect pw, DrawingObjects:=True, Contents:=True, Scenarios:=True _
                    , AllowFiltering:=True
                    
                Select Case Err.Number
                Case Is <> 0: Debug.Print Err.Description
                End Select
                Err.Clear
                
            Case Else
                MsgBox "Sheet " & sht & " not found!"
                Err.Clear
            End Select
        End With
    Next
    Hide = True
End Function

Private Function Unhide(pw As String, ParamArray sheetname() As Variant) As Boolean
On Error Resume Next
    For Each sht In sheetname
        With Sheets(sht)
            Select Case Err.Number
            Case 0
                .Unprotect Password:=pw
                .Cells.EntireRow.hidden = False
                If Err.Number <> 0 Then
                    MsgBox "Wrong password", vbExclamation
                    Exit Function
                End If
            Case Else
                MsgBox "Sheet " & sht & " not found!"
                Err.Clear
            End Select
        End With
    Next
    Unhide = True
End Function

The second one is this one
VBA Code:
Sub Stuepef()
Application.ScreenUpdating = False
   Dim Ary As Variant
   Dim i As Long
  
   Ary = Array(London, "26,28,39,142", Paris, "135,147,158,200,201,202", Sheet8, "26:75", Sheet3, "11:239")
   For i = 0 To UBound(Ary) Step 2
      Ary(i).Rows(Ary(i + 1)).Hidden = False
   Next i
End Sub
I want to add in the First, this condition from the Second, to be able to say in which, exactly worksheet, exactly which rows to hide
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think it's better that you explain your needs in detail, forgetting about the "quasi-solution" you identified...
 
Upvote 0
I think it's better that you explain your needs in detail, forgetting about the "quasi-solution" you identified...
Hello,
what I want is in the first macro, to change instead of all described worksheets to hide the mentioned rows, to be the same as action, but for each (as in the second macro) worksheet to be different rows.
For example: in Paris - 2,5,7,9,15 rows
London - 25,34,36,38,39,42,47 rows
etc
How do I change this

VBA Code:
If Hide("Plovdiv", "London", "NY", "Paris") Then

If Unhide(InputBox("Enter password"), "London", "NY", "Paris") Then

.Range( _
                    "4:4,5:5,10:10,11:11,13:13,14:14,17:17,18:18,34:34,35:35,38:38," & _
                    "45:45,46:46,49:49,50:50,72:72,73:73,76:76,77:77,78:78,79:79" _
                    ).EntireRow.hidden = True
to this
Code:
Ary = Array(London, "26,28,39,142", Paris, "135,147,158,200,201,202", Sheet8, "26:75", Sheet3, "11:239")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top