简体   繁体   中英

VBA: hide and unhide code not working

I'm new to VBA and have been trying to write a code that hides and unhides rows based on the input value of a certain cell address. However, it doesn't work and I don't why. I have posted my code below:

Sub Hide()

If Worksheets("IS").Range("B8").Value = "Show All" Then
   Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
End If

If Worksheets("IS").Range("B8").Value = "Just Revenue" Then
   Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
   Worksheets("IS").Rows("28:165").EntireRow.Hidden = True
End If

If Worksheets("IS").Range("B8").Value = "Just Expenses" Then
   Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
   Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
   Worksheets("IS").Rows("160:165").EntireRow.Hidden = True
End If

If Worksheets("IS").Range("B8").Value = "Just Cogs" Then
   Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
   Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
   Worksheets("IS").Rows("64:165").EntireRow.Hidden = True
End If

If Worksheets("IS").Range("B8").Value = "Just Totals" Then
   Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
   Worksheets("IS").Rows("12:25").EntireRow.Hidden = True
   Worksheets("IS").Rows("28:61").EntireRow.Hidden = True
   Worksheets("IS").Rows("64:91").EntireRow.Hidden = True
   Worksheets("IS").Rows("93:155").EntireRow.Hidden = True
End If

End Sub

Any help with why my code doesn't work or any tips to improve it would be much appreciative.

Rewriting for Worksheet_Change:

In your VBE, paste this code into the code sheet for the "IS" worksheet (double click it in the Project - VBAProject pane. If the Project - VBAProject pane is not visible in your VBE, go to View >> Project Explorer ):

Private Sub Worksheet_Change(ByVal Target As Range)

    'Ensure that we don't trigger another change event while this code is running
    Application.EnableEvents = False

    'Check if cell B8 triggered this change:
    If Not Intersect(Target, Range("B8")) Is Nothing Then

       'B8 changed... which means B8 is "Target" variable
        Select Case Target.Value
            Case "Show All"
                Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
            Case "Just Revenue"
                Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
                Worksheets("IS").Rows("28:165").EntireRow.Hidden = True
            Case "Just Expenses"
                Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
                Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
                Worksheets("IS").Rows("160:165").EntireRow.Hidden = True
            Case "Just Cogs"
                Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
                Worksheets("IS").Rows("12:27").EntireRow.Hidden = True
                Worksheets("IS").Rows("64:165").EntireRow.Hidden = True
            Case "Just Totals"
                Worksheets("IS").Rows("12:165").EntireRow.Hidden = False
                Worksheets("IS").Rows("12:25").EntireRow.Hidden = True
                Worksheets("IS").Rows("28:61").EntireRow.Hidden = True
                Worksheets("IS").Rows("64:91").EntireRow.Hidden = True
                Worksheets("IS").Rows("93:155").EntireRow.Hidden = True
        End Select
    End If

    'Turn events back on so this code triggers again
    Application.EnableEvents = True
End Sub

There are quite a few events that we can hook VBA to (SelectionChange, DoubleClick, Workbook_Close, etc). In this case we are hooking to Worksheet_Change() .

This code gets triggered every time this worksheet experiences a change. The Target variable will hold the range that triggered the event. So we test to see if that Target intersects with Range("B8") which means B8 was changed. Then we perform the code inside the If block.

I switched your If/ElseIf over to a Select/Case just because it makes for cleaner code since we are testing a single condition (the value of B8) over and over again.

In this code we also toggle off the Excel Applications EnableEvents feature. This feature is what allowed this Worksheet_Change() to get triggered in the first place. Often times in the code we make more changes to the worksheet (hiding rows or columns, for instance) which will trigger the application to run Worksheet_Change() again... while it's running Worksheet_Change() already. This can cause code to run superfluously and, often, cause an endless loop that makes excel crash.

This code needs to be pasted on the sheet where you want are wanting to execute the code. You will not need to qualify your ranges with the sheets once the code is there as well.

You can just refer directly to your range without the Worksheets("IS"). as so:
Rows("so and so").EntireRow.Hidden = True

You can also just refer to your TargetRange by variable now like so:
If MyTarget = "Just Revenue" Then

I inserted one of your conditions in the code as an example

Option Explicit

Private Sub worksheet_change(ByVal target As Range)

Dim MyTarget As Range
Set MyTarget = Range("B8")

If Not Intersect(target, MyTarget) Is Nothing Then
Application.EnableEvents = False

'Your CODE HERE
If MyTarget = "Show All" Then
Rows("12:165").EntireRow.Hidden = False
End If

Application.EnableEvents = True
End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM