如何将 Excel 上的复选框链接到 VBA 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17237296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to link a checkbox on Excel to a VBA code
提问by user2453446
I have created a checkbox on my Excel Work sheet, using design mode I have leftclicked it and named it ChkV, and I wrote a VBA code but when I run it I get an message telling that the variable is not defined.
我在我的 Excel 工作表上创建了一个复选框,使用设计模式我左键单击它并将其命名为 ChkV,我编写了一个 VBA 代码,但是当我运行它时,我收到一条消息,告诉我变量未定义。
If ChkV.Value = True Then
' my code
End If
Did I not label the check box correctly, what am I doing wrong ? How should I fix the mistake?
我没有正确标记复选框,我做错了什么?我应该如何修复错误?
回答by Pawzik
Should it not be
应该不是
If activesheet.Checkboxes("ChkV") = xlOn Then
'your code
End If
?
?
回答by Kazimierz Jawor
You have this error when you call your code outside Sheet module
where your checkbox
is located. To improve your code you need to add references to sheet where checkbox
belongs to, like:
当您Sheet module
在您checkbox
所在的位置之外调用您的代码时,您会遇到此错误。要改进您的代码,您需要添加对工作表checkbox
所属的引用,例如:
If Sheets("Sheet 1").ChkV.Value = True Then
' my code
End If
回答by Rafa Barragan
Hi i was breaking my head over and over again for this, but searching i found that you need to refer by the CodeName
's sheet or using OleObjects
嗨,我为此一遍又一遍地打断我的头,但是搜索我发现您需要参考CodeName
表格或使用OleObjects
By
Code Name
is how you see in the VBA Project tree:Hoja1.[CheckBoxName].Value '' In English or what ever you may call your sheet is: Sheet1.[CheckBoxName].Value
By
OleObjects
is:Dim Wks as Worksheet Set Wks = Worksheets("[SheetName]") '' in this case "Prueba" Wks.OLEObjects("[CheckBoxName]").Object.Value
By
Code Name
是您在VBA 项目树中看到的方式:Hoja1.[CheckBoxName].Value '' In English or what ever you may call your sheet is: Sheet1.[CheckBoxName].Value
通过
OleObjects
是:Dim Wks as Worksheet Set Wks = Worksheets("[SheetName]") '' in this case "Prueba" Wks.OLEObjects("[CheckBoxName]").Object.Value
Note that my Excel is in Spanish thats why you see Hoja1
, in English is Sheet1
and you can find something else here.
请注意,我的 Excel 是西班牙语的,这就是您看到的原因Hoja1
,英语是Sheet1
,您可以在此处找到其他内容。