使用表单控件复选框而不是ActiveX触发VBA代码

时间:2015-05-11 16:14:22

标签: vba activex form-control

我在下面的代码中使用预算模板进行工作。但是,在我向所有用户推出之前,我需要让Mac友好。由于Mac不喜欢ActiveX控件,我试图找到一种方法,使用表单控件使这段代码工作。有人对我有什么想法吗? 谢谢!

Private Sub travelcalc_Click()
ActiveSheet.Unprotect Password:="PASSWORD"
    If Me.travelcalc.Value = False Then
    Range("C19:L20").Locked = False
    Sheets("Travel Calculator").Visible = False
Else
    Sheets("Travel Calculator").Visible = True
    Range("$C$19") = Worksheets("Travel Calculator").Range("$N$25")
    Range("$D$19") = Worksheets("Travel Calculator").Range("$P$25")
    Range("$E$19") = Worksheets("Travel Calculator").Range("$R$25")
    Range("$F$19") = Worksheets("Travel Calculator").Range("$T$25")
    Range("$G$19") = Worksheets("Travel Calculator").Range("$V$25")
    Range("$H$19") = Worksheets("Travel Calculator").Range("$X$25")
    Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$25")
    Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$25")
    Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$25")
    Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$25")
    Range("$C$20") = Worksheets("Travel Calculator").Range("$N$51")
    Range("$D$20") = Worksheets("Travel Calculator").Range("$P$51")
    Range("$E$20") = Worksheets("Travel Calculator").Range("$R$51")
    Range("$F$20") = Worksheets("Travel Calculator").Range("$T$51")
    Range("$G$20") = Worksheets("Travel Calculator").Range("$V$51")
    Range("$H$20") = Worksheets("Travel Calculator").Range("$X$51")
    Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$51")
    Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$51")
    Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$51")
    Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$51")
    Range("C19:L20").Locked = True
End If
ActiveSheet.Protect Password:="PASSWORD", AllowFormattingColumns:=True, AllowFormattingRows:=True

End Sub

2 个答案:

答案 0 :(得分:0)

插入表单控件后,右键单击它,然后选择“指定宏”,并选择“新建”。将为其“点击事件”分配一个宏。

检查它是否已被检查有点棘手,但可行:

Private Sub CheckBox1_Click() 'assign this macro to your checkbox; make sure it is in a standard module
ActiveSheet.Unprotect Password:="PASSWORD"

If ActiveSheet.Shapes("Check Box 1").OLEFormat.Object.Value <> 1 Then
  Range("C19:L20").Locked = False
  Sheets("Travel Calculator").Visible = False
Else
  yadda-yadda your code here...
End if

ActiveSheet.Protect Password:="PASSWORD", AllowFormattingColumns:=True, AllowFormattingRows:=True
End Sub

说实话,我只能希望这适用于MAC,从未尝试过。但是如果你说Form控件工作正常,那么应该这样做......

答案 1 :(得分:0)

分配给复选框的这个宏工作得很好。

Sub CheckBox11_Click()
ActiveSheet.Unprotect Password:="PASSWORD"
    If ActiveSheet.Shapes("Check Box 11").OLEFormat.Object.Value <> 1 Then
    Range("C19:L20").Locked = False
        Sheets("Travel Calculator").Visible = False
Else
    Sheets("Travel Calculator").Visible = True
    Range("$C$19") = Worksheets("Travel Calculator").Range("$N$25")
    Range("$D$19") = Worksheets("Travel Calculator").Range("$P$25")
    Range("$E$19") = Worksheets("Travel Calculator").Range("$R$25")
    Range("$F$19") = Worksheets("Travel Calculator").Range("$T$25")
    Range("$G$19") = Worksheets("Travel Calculator").Range("$V$25")
    Range("$H$19") = Worksheets("Travel Calculator").Range("$X$25")
    Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$25")
    Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$25")
    Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$25")
    Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$25")
    Range("$C$20") = Worksheets("Travel Calculator").Range("$N$51")
    Range("$D$20") = Worksheets("Travel Calculator").Range("$P$51")
    Range("$E$20") = Worksheets("Travel Calculator").Range("$R$51")
    Range("$F$20") = Worksheets("Travel Calculator").Range("$T$51")
    Range("$G$20") = Worksheets("Travel Calculator").Range("$V$51")
    Range("$H$20") = Worksheets("Travel Calculator").Range("$X$51")
    Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$51")
    Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$51")
    Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$51")
    Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$51")
    Range("C19:L20").Locked = True
End If
ActiveSheet.Protect Password:="PASSWORD", AllowFormattingColumns:=True, AllowFormattingRows:=True

End Sub