Hide rows (in all sheets) base on cell value

cambralenta

New Member
Joined
Nov 30, 2009
Messages
23
Hello,
I'm using this BruceWayne code that works fine in the worksheet whith the command button. But, I want the macro to run in all sheets except were the command button is?

Code:
Sub RowsToHide()
Dim myVal   As String
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long


mainRow = 38
myVal = Range("C9").Value


If myVal = 30 Then
    Rows(mainRow - 30 & ":" & mainRow - 30 + myVal).EntireRow.Hidden = False
ElseIf myVal >= 1 And myVal <= 29 Then
    tweakRow = mainRow - 30
    row1 = (mainRow - (29 - myVal))
    row2 = (mainRow - (30 - myVal))
    Set hideRange = Rows(row1 & ":" & mainRow).EntireRow
    Set showRange = Rows(tweakRow & ":" & row2).EntireRow


    Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address


    hideRange.Hidden = True
    showRange.Hidden = False
ElseIf myVal = 0 Then
    Rows(mainRow - 30 & ":" & mainRow).EntireRow.Hidden = True
End If
End Sub
All sheets are password protected! Button is in Worksheet ("DATA").
Have tried, with many searches but i can't do it by my self!... Thanks
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
try this

Code:
Sub RowsToHide()
Dim myVal  As String, sh As Worksheet
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
For Each sh In ThisWorkbook.Sheets
    If sh.Name <> "Data" Then
        mainRow = 38
        myVal = Range("C9").Value
        If myVal = 30 Then
            Rows(mainRow - 30 & ":" & mainRow - 30 + myVal).EntireRow.Hidden = False
        ElseIf myVal >= 1 And myVal <= 29 Then
            tweakRow = mainRow - 30
            row1 = (mainRow - (29 - myVal))
            row2 = (mainRow - (30 - myVal))
            Set hideRange = Rows(row1 & ":" & mainRow).EntireRow
            Set showRange = Rows(tweakRow & ":" & row2).EntireRow
            Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
            hideRange.Hidden = True
            showRange.Hidden = False
        ElseIf myVal = 0 Then
            Rows(mainRow - 30 & ":" & mainRow).EntireRow.Hidden = True
        End If
    End If
Next
End Sub
 
Last edited:
Upvote 0
So, I changed it for this and it works like I want.
Just don´t know were to put the Unprotect/Protect code to run in all protected sheets!


Code:
Sub RowsToHide()Dim myVal  As String, sh As Worksheet
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
For Each sh In ThisWorkbook.Sheets
    If sh.Name <> "Folha1" Then
        mainRow = 38
        myVal = Range("C9").Value
        If myVal = 30 Then
            [B]sh.[/B]Rows(mainRow - 30 & ":" & mainRow - 30 + myVal).EntireRow.Hidden = False
        ElseIf myVal >= 1 And myVal <= 29 Then
            tweakRow = mainRow - 30
            row1 = (mainRow - (29 - myVal))
            row2 = (mainRow - (30 - myVal))
            Set hideRange = [B]sh.[/B]Rows(row1 & ":" & mainRow).EntireRow
            Set showRange = [B]sh.[/B]Rows(tweakRow & ":" & row2).EntireRow
            Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
            hideRange.Hidden = True
            showRange.Hidden = False
        ElseIf myVal = 0 Then
            [B]sh.[/B]Rows(mainRow - 30 & ":" & mainRow).EntireRow.Hidden = True
        End If
    End If
Next
End Sub
 
Last edited:
Upvote 0
So, It's done! Ended like this:

Code:
Sub RowsToHide()Dim myVal  As String, sh As Worksheet
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
For Each sh In ThisWorkbook.Sheets
[COLOR=#ff0000]    sh.Unprotect Password:=""[/COLOR]
    If sh.Name <> "Data" Then
        mainRow = 38
        myVal = [COLOR=#ff0000]Worksheets("Data")[/COLOR].Range("A1").Value
        If myVal = 30 Then
            [COLOR=#ff0000]sh.[/COLOR]Rows(mainRow - 30 & ":" & mainRow - 30 + myVal).EntireRow.Hidden = False
        ElseIf myVal >= 1 And myVal <= 29 Then
            tweakRow = mainRow - 30
            row1 = (mainRow - (29 - myVal))
            row2 = (mainRow - (30 - myVal))
            Set hideRange = [COLOR=#ff0000]sh.[/COLOR]Rows(row1 & ":" & mainRow).EntireRow
            Set showRange = [COLOR=#ff0000]sh.[/COLOR]Rows(tweakRow & ":" & row2).EntireRow
            Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
            hideRange.Hidden = True
            showRange.Hidden = False
        ElseIf myVal = 0 Then
            [COLOR=#ff0000]sh.[/COLOR]Rows(mainRow - 30 & ":" & mainRow).EntireRow.Hidden = True
        
        End If
[COLOR=#ff0000]sh.Protect Password:=""[/COLOR]
    End If
Next
End Sub

Thanks JLGWhiz ...
 
Upvote 0
So, It's done! Ended like this:

Code:
Sub RowsToHide()Dim myVal  As String, sh As Worksheet
Dim mainRow As Long, tweakRow As Long
Dim hideRange As Range, showRange As Range
Dim row1 As Long, row2 As Long
For Each sh In ThisWorkbook.Sheets
[COLOR=#ff0000]    sh.Unprotect Password:=""[/COLOR]
    If sh.Name <> "Data" Then
        mainRow = 38
        myVal = [COLOR=#ff0000]Worksheets("Data")[/COLOR].Range("A1").Value
        If myVal = 30 Then
            [COLOR=#ff0000]sh.[/COLOR]Rows(mainRow - 30 & ":" & mainRow - 30 + myVal).EntireRow.Hidden = False
        ElseIf myVal >= 1 And myVal <= 29 Then
            tweakRow = mainRow - 30
            row1 = (mainRow - (29 - myVal))
            row2 = (mainRow - (30 - myVal))
            Set hideRange = [COLOR=#ff0000]sh.[/COLOR]Rows(row1 & ":" & mainRow).EntireRow
            Set showRange = [COLOR=#ff0000]sh.[/COLOR]Rows(tweakRow & ":" & row2).EntireRow
            Debug.Print "For a value of " & myVal & ", we will hide range: " & hideRange.Address & ", and show range: " & showRange.Address
            hideRange.Hidden = True
            showRange.Hidden = False
        ElseIf myVal = 0 Then
            [COLOR=#ff0000]sh.[/COLOR]Rows(mainRow - 30 & ":" & mainRow).EntireRow.Hidden = True
        
        End If
[COLOR=#ff0000]sh.Protect Password:=""[/COLOR]
    End If
Next
End Sub

Thanks JLGWhiz ...
Glad you got it working. My haste in replying without testing does not yield the best results, but you were able to resolve it, so all is well.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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