选项按钮的链接单元格更改时的操作

时间:2017-06-16 15:18:51

标签: excel vba excel-vba excel-formula excel-2010

我的选项按钮链接的单元格出现问题。我想在链接单元格的值发生变化时加载宏。 我尝试了两种不同的方法,当按钮更改值时,它们都不起作用。如果我在一个选定的单元格中键入一个值,我的宏就会加载。

以下是我的两种技巧:

Private Cel_CONGESg As Byte
Private Sub Worksheet_Calculate()
Dim Ws As Worksheet

Set Ws = ThisWorkbook.Sheets("Externe")

If Ws.Range("$I$12").Value <> Cel_CONGESg Then
    MsgBox "heheheheeheheheheee"
End If

Cel_CONGESg = Ws.Range("$I$12").Value

End Sub

第二种方法

Private Sub Worksheet_Change(ByVal target As Range)
Dim CongesG
Dim Ws As Worksheet

Set CongesG = Ws.Range("$I$12")

If Not Application.Intersect(CongesG, Range(target.Address)) _
       Is Nothing Then
    If Ws.Range("$I$12").Value = 2 Or Ws.Range("$I$12").Value = 0 Then
        Ws.Range("$I$13").EntireRow.Hidden = True
            With Range("H12:L12").Borders(xlEdgeBottom)
                .LineStyle = xlDot
                .Color = RGB(51, 63, 79)
                .Weight = xlThin
            End With
    ElseIf Ws.Range("$I$12").Value = 1 Then
        Ws.Range("$I$13").EntireRow.Hidden = False
            With Range("H12:L12").Borders(xlEdgeBottom)
                .LineStyle = xlNone
            End With
    End If 'I12 Congés
End If 'Application intersect CongesG
End Sub

你能帮我理解这是什么问题吗?

提前谢谢你。祝你有美好的一天。

enter image description here

1 个答案:

答案 0 :(得分:0)

您没有使用Worksheet_Change事件的全部功能,Target已经定义为Range

尝试以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim CongesG As Range

Set CongesG = Range("$I$12")

If Not Application.Intersect(CongesG, Target) Is Nothing Then
    Application.EnableEvents = False
    Select Case Target.Value
        Case 2, 0
            Target.Offset(1).EntireRow.Hidden = True
            With Range("H12:L12").Borders(xlEdgeBottom)
                .LineStyle = xlDot
                .Color = RGB(51, 63, 79)
                .Weight = xlThin
            End With
        Case 1
            Target.Offset(1).EntireRow.Hidden = False
            With Range("H12:L12").Borders(xlEdgeBottom)
                .LineStyle = xlNone
            End With
    End Select
End If 'Application intersect CongesG
Application.EnableEvents = True

End Sub