问题
I have a problem with my option buttons linked cells. I'd like to load a macro whenever the value of the linked cell changes. I tried two different methods, and none of them won't work when the button changes the value. If I type in a selected cell a value, my macros do load.
Here are my two techniques:
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
The second method
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
Could you please help me understand what's the problem?
Thank you in advance. Have a great day.
Jean
回答1:
You are not using the full capabilities of the Worksheet_Change
event, and Target
, which is already defined as Range
.
Try the code below:
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
来源:https://stackoverflow.com/questions/44592776/action-when-linked-cell-of-option-button-changes