vba 如何在vba excel中创建一种变量Checkbox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14750089/
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 create a kind of variable Checkbox in vba excel
提问by Jonathan Raul Tapia Lopez
I have the next excel sheet with many checkboxs.
我有下一个带有许多复选框的 Excel 工作表。
The problem is when I am coding I have to do some functions with Valor1and Valor2depending of if the checkboxis activate.
问题是当我编码时,我必须使用Valor1和Valor2执行一些功能,具体取决于复选框是否被激活。
Well I have the code.
好吧,我有代码。
Option Explicit
Sub Casilladeverificación1_Haga_clic_en()
Range("c12").Activate
Do
If CheckBox1.Value Then
Call fucntion1
'Works for the first row, but for the second row int shoul be check CheckBox12 ,a next CheckBox23 ...
If CheckBox2.Value Then
Call fucntion1
If CheckBox2.Value Then
Call fucntion3
....
ActiveCell.Offset(1, 0).Activate
While Not IsEmpty(ActiveCell.Value2)
End Sub
But you can notice I dont want to made all the case with all the checkbox, there is a solve for this like checkbox[i]
但是你会注意到我不想用所有的复选框来解决所有的问题,有一个解决方案,比如checkbox[i]
采纳答案by Bmo
I would put all of your functions into one big function and the functionality would separated by a Select Case
block.
我会将您的所有功能放入一个大功能中,并且功能将被一个Select Case
块分隔。
Private Sub functionRouter(checkAction as integer)
Select Case checkAction
Case 1
'Code for function one
Case 2
'Code for function two
''Etc.
End Select
End Sub
You're going to want to loop over all your check boxes. This is going to depend on what checkbox you are using.
您将要遍历所有复选框。这将取决于您使用的复选框。
Sub test()
Dim chkBox As CheckBox
Dim chkBox2 As OLEObject
'Regular
For Each chkBox In Sheets("Sheet1").CheckBoxes
Debug.Print chkBox.Caption
Next chkBox
'ActiveX
For Each chkBox2 In Sheets("Sheet1").OLEObjects
If TypeName(chkBox2.Object) = "CheckBox" Then
Debug.Print chkBox2.Object.Value
End If
Next chkBox2
You could do a few different things with all of your checkboxes. You could use the tag
property (you would need to set all of them, but this allows for duplicates). Then call functionRouter(chkBox.tag)
Or you could parse something from the name functionRouter Right(chkBox.name, 1)
您可以对所有复选框执行一些不同的操作。您可以使用该tag
属性(您需要设置所有这些属性,但这允许重复)。然后调用functionRouter(chkBox.tag)
或者你可以从名字中解析出一些东西functionRouter Right(chkBox.name, 1)
回答by Andrey Gordeev
You can iterate checkboxes on worksheet by using this loop:
您可以使用此循环迭代工作表上的复选框:
For Each chk In ActiveSheet.CheckBoxes
MsgBox chk.Name
Next
It won't work if you use ActiveX
controls though.
但是,如果您使用ActiveX
控件,它将不起作用。