简体   繁体   中英

Clear contents of cell based on value in other cell

I need some help making my VBA code work. The first part where it hides certain rows works but when I try to clear the contents of cells based on a value in another cell I can not seem to get it to work.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)`
Dim c As Range
On Error Resume Next
 If Target.Address = "$H$5" Then
    Rows("10:109").EntireRow.Hidden = False

    Select Case Target.Value
        Case "1"
            Rows("30:109").EntireRow.Hidden = True

    End Select

    Select Case Target.Value
        Case "2"
            Rows("50:109").EntireRow.Hidden = True

    End Select

    Select Case Target.Value
        Case "3"
            Rows("70:109").EntireRow.Hidden = True

    End Select
 End If

 If Target.Address = "$H$6" Then

    Select Case Target.Value
        Case "1"
            Range("C18:D23").Cells.ClearContents

    End Select

    Select Case Target.Value
        Case "2"
            Range("C19:D23").Cells.ClearContents

    End Select

    Select Case Target.Value
        Case "3"
            Range("C20:D23").Cells.ClearContents

    End Select
  End If
End Sub

May be

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range

On Error Resume Next

If Target.Address = "$H$5" Then
    Application.EnableEvents = False
        Rows("10:109").EntireRow.Hidden = False

        Select Case Target.Value
            Case "1"
                Rows("30:109").EntireRow.Hidden = True
            Case "2"
                Rows("50:109").EntireRow.Hidden = True
            Case "3"
                Rows("70:109").EntireRow.Hidden = True
        End Select
    Application.EnableEvents = True
End If

If Target.Address = "$H$6" Then
    Application.EnableEvents = False
        Select Case Target.Value
            Case "1"
                Range("C18:D23").Cells.ClearContents
            Case "2"
                Range("C19:D23").Cells.ClearContents
            Case "3"
                Range("C20:D23").Cells.ClearContents
        End Select
    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