简体   繁体   中英

Hide and unhide rows in Excel VBA when specific value in the one cell

I want to hide/unhide if a specific value is selected from a drop down list. As long as it works, it could be under the worksheet code (when the value is selected) or when a button is pushed. Your help is greatly appreciated. I have tried with this code unsuccessfully..

Application.EnableEvents = False

If DWR.Cells(4, 14) = "CANTI" Then
    DWR.Activate
    DWR.Range("10:49").EntireRow.Hidden = False
    'must hide the empty rows
    DWR.Activate
    DWR.Range("50:89").EntireRow.Hidden = True

ElseIf DWR.Cells(4, 14) = "F100" Then
    DWR.Activate
    DWR.Range("50:89").EntireRow.Hidden = True
    'must hide the empty rows
    DWR.Activate
    DWR.Range("10:49").EntireRow.Hidden = False

End If

Application.EnableEvents = True

Any suggestions?

Try something like this?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B1")) Is Nothing Then

        Application.EnableEvents = False

            Range("10:89").EntireRow.Hidden = False     '~~~> Default case: display ALL rows

            If Target = "CANTI" Then
                Range("10:49").EntireRow.Hidden = False
                Range("50:89").EntireRow.Hidden = True
            ElseIf Target = "F100" Then
                Range("10:49").EntireRow.Hidden = True
                Range("50:89").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