根据其他单元格中的值清除单元格内容

时间:2018-08-10 07:36:58

标签: excel vba excel-vba

我需要一些帮助使我的VBA代码正常工作。它隐藏某些行的第一部分有效,但是当我尝试根据另一个单元格中的值清除单元格的内容时,我似乎无法使其正常工作。

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

1 个答案:

答案 0 :(得分:-1)

可能是

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