我需要根据变量列表禁用复选框。例如,如果用户在列表框中选择“西班牙”,则仅应允许使用某些复选框货币。
我当前的代码是:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range, c As Range, cb As OLEObject
' set a range where the cell will be either 1 or 0 depending on country selection
Set R = Me.Range("C115:C135")
If Not Intersect(Target, R) Is Nothing Then
' sets the checbox to zero if related cell value in range is zero
For Each c In Intersect(Target, R)
For Each cb In Me.OLEObjects
' Identifies the relevant Checkbox name based on related cell address
If cb.Name Like "CheckBox" & Val(Split(c.Address, "$")(2)) - 14 Then
cb.Enabled = c.Value > 0
End If
Next cb
Next c
End If
End Sub
答案 0 :(得分:0)
您需要在这里有点棘手。
首先,在所需的工作表中,您已在其中粘贴了代码,编写一个新的子程序,如下所示:
Sub MyMacro()
Call Worksheet_Change(ActiveCell) 'change ActiveCell for the range you want to be the target
End Sub
然后,从列表框的事件Change
中尝试:
Private Sub ComboBox1_Change()
Call Hoja1.MyMacro 'Replace Hoja1 with the name of your worksheet in VBA.
End Sub
答案 1 :(得分:0)
我实际上能够修改宏,因此可以直接在模块中使用它,也可以直接从列表框中调用它。见下文。谢谢大家的指教。 抱歉,我难以缩进完整的代码。
Public Sub Checkboxupdate()
Dim R As Range, c As Range, cb As OLEObject, hb As CheckBox
Set R = Range("C115:C135")
If Not R Is Nothing Then
For Each c In R
On Error Resume Next
For Each cb In ActiveSheet.OLEObjects
If cb.Name Like "CheckBox" & Val(Split(c.Address, "$")(2)) - 14 Then
cb.Enabled = c.Value > 0
End If
Next cb
Next c
End If
MsgBox "Currency availability updated.", vbOKOnly
End Sub