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

Triggering VBA code using Form Control Check Box instead of ActiveX

我在工作预算模板中使用了以下代码。但是,在我向所有用户推出此功能之前,我需要使其变得 Mac 友好。由于 Macs 不喜欢 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") = Worksheets("Travel Calculator").Range("$N")
    Range("$D") = Worksheets("Travel Calculator").Range("$P")
    Range("$E") = Worksheets("Travel Calculator").Range("$R")
    Range("$F") = Worksheets("Travel Calculator").Range("$T")
    Range("$G") = Worksheets("Travel Calculator").Range("$V")
    Range("$H") = Worksheets("Travel Calculator").Range("$X")
    Range("$I") = Worksheets("Travel Calculator").Range("$Z")
    Range("$J") = Worksheets("Travel Calculator").Range("$AB")
    Range("$K") = Worksheets("Travel Calculator").Range("$AD")
    Range("$L") = Worksheets("Travel Calculator").Range("$AF")
    Range("$C") = Worksheets("Travel Calculator").Range("$N")
    Range("$D") = Worksheets("Travel Calculator").Range("$P")
    Range("$E") = Worksheets("Travel Calculator").Range("$R")
    Range("$F") = Worksheets("Travel Calculator").Range("$T")
    Range("$G") = Worksheets("Travel Calculator").Range("$V")
    Range("$H") = Worksheets("Travel Calculator").Range("$X")
    Range("$I") = Worksheets("Travel Calculator").Range("$Z")
    Range("$J") = Worksheets("Travel Calculator").Range("$AB")
    Range("$K") = Worksheets("Travel Calculator").Range("$AD")
    Range("$L") = Worksheets("Travel Calculator").Range("$AF")
    Range("C19:L20").Locked = True
End If
ActiveSheet.Protect Password:="PASSWORD", AllowFormattingColumns:=True, AllowFormattingRows:=True

End Sub

插入表单控件后,右击它,select分配宏,select新建。宏将分配给它的 "click event".

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

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 上有效,但从未尝试过。但是如果你说表单控件工作正常,那么这应该可以......

这个分配给复选框的宏非常有效。

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") = Worksheets("Travel Calculator").Range("$N")
    Range("$D") = Worksheets("Travel Calculator").Range("$P")
    Range("$E") = Worksheets("Travel Calculator").Range("$R")
    Range("$F") = Worksheets("Travel Calculator").Range("$T")
    Range("$G") = Worksheets("Travel Calculator").Range("$V")
    Range("$H") = Worksheets("Travel Calculator").Range("$X")
    Range("$I") = Worksheets("Travel Calculator").Range("$Z")
    Range("$J") = Worksheets("Travel Calculator").Range("$AB")
    Range("$K") = Worksheets("Travel Calculator").Range("$AD")
    Range("$L") = Worksheets("Travel Calculator").Range("$AF")
    Range("$C") = Worksheets("Travel Calculator").Range("$N")
    Range("$D") = Worksheets("Travel Calculator").Range("$P")
    Range("$E") = Worksheets("Travel Calculator").Range("$R")
    Range("$F") = Worksheets("Travel Calculator").Range("$T")
    Range("$G") = Worksheets("Travel Calculator").Range("$V")
    Range("$H") = Worksheets("Travel Calculator").Range("$X")
    Range("$I") = Worksheets("Travel Calculator").Range("$Z")
    Range("$J") = Worksheets("Travel Calculator").Range("$AB")
    Range("$K") = Worksheets("Travel Calculator").Range("$AD")
    Range("$L") = Worksheets("Travel Calculator").Range("$AF")
    Range("C19:L20").Locked = True
End If
ActiveSheet.Protect Password:="PASSWORD", AllowFormattingColumns:=True, AllowFormattingRows:=True

End Sub