宏在单元格值更改时触发 - 使用' Worksheet_Change' VBA

时间:2017-12-12 15:38:38

标签: excel vba excel-vba

我试图编写一个vba脚本,根据单元格值的变化触发各种宏。对于单个单元格值更改,下面的代码为' C22'细胞工作正常 - 但当我添加额外的细胞时,它给了我一个错误。

有什么想法吗?


Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Address = "$C$22" Then
    Select Case Target.Value
        Case "Yes"
            Call Hide280C
        Case "No"
            Call Show280C
        Case "Not Sure"
            Call Show280C
        Case ""
            Call Show280C
        Case Else
            Exit Sub
    End Select

If Target.Address = "$L$33" Then
    Select Case Target.Value
        Case "Yes"
            Call NJCreditShow
        Case "No"
            Call NJCreditHide
        Case Else
            Exit Sub
    End Select

If Target.Address = "$L$34" Then
    Select Case Target.Value
        Case "Yes"
            Call PACreditShow
        Case "No"
            Call PACreditHide
        Case Else
            Exit Sub
    End Select

End If

End Sub

1 个答案:

答案 0 :(得分:0)

将您的代码更新为:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$22" Then
    Select Case Target.Value
        Case "Yes"
            'Call Hide280C (Call not needed)
            Hide280c
        Case "No"
            Show280C
        Case "Not Sure"
            Show280C
        Case ""
            Show280C
        Case Else
            Exit Sub
    End Select
ElseIf Target.Address = "$L$33" Then 'You missed the End if, but you could also use ElseIf and only have one End if at the end.
    Select Case Target.Value
        Case "Yes"
            NJCreditShow
        Case "No"
            NJCreditHide
        Case Else
            Exit Sub
    End Select
ElseIf Target.Address = "$L$34" Then
    Select Case Target.Value
        Case "Yes"
            PACreditShow
        Case "No"
            PACreditHide
        Case Else
            Exit Sub
    End Select
End if
End Sub