VBA 显示选择选项
我正在尝试编写一个代码,该代码将根据选中的复选框显示一个值。总共有 5 个复选框,我将来会添加其他复选框,所以我想知道是否有一种简单的方法来确定选中哪些复选框来确定要显示哪些值。我可以用一种非常迂回的方式来做到这一点,但如果可能的话,我想最小化我的代码。
换句话说,如果我写出每个场景,我将不得不为所有不同的选择可能性编写单独的代码:
仅 1、仅 2、仅 3、仅 4、仅 5
1+2、1+3、1+ 4、1+5、2+3、2+4、2+5、3+4、3+5、4+5
1+2+3、1+2+4、1+2+5、 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5 , 1+3+4+5, 2+3+4+5
1+2+3+4+5
每个值都与一个子项关联,如果选择该子项,该子项将填充数组。填充数组后,我需要对选定的数组执行附加功能。执行的功能是相同的,但如果未选择值,我不想执行该功能,因为否则它将破坏我的功能的目的。该函数本身是从被选择到另一个数组中的数组中选择重复项。
I am trying to write a code that will display a value depending on what checkbox is selected. There are a total of 5 checkboxes and I will be adding additional checkboxes in the future so I was wondering if there is an easy way to determine which checkboxes are checked to determine which values to display. I can do this in a really round about way but I would like minimize my code if possible.
In other words, if i write each scenario out I would have to write a separate code for all of the different selection possbilities:
1 only,2 only,3 only,4 only,5 only
1+2, 1+3, 1+4, 1+5, 2+3, 2+4, 2+5, 3+4, 3+5, 4+5
1+2+3, 1+2+4,1+2+5, 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5, 1+3+4+5, 2+3+4+5
1+2+3+4+5
Each value is associated with a sub that will fill the array if it is selected. And after the arrays are filled I need to perform an additional function on the ones that are selected. The function performed is the same but I do not want to perform the function if a value is not selected because it will defeat the purpose of my function otherwise. The function itself is to select duplicates from the arrays that were selected into another array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为每个复选框使用二进制数:
=2
0
)=2
1
)=2
2
)=2
2
)=2
4
)因此,当您对每个排列求和时,您将得到一个唯一的数字可以检查。
0
,2仅是1
,依此类推3
,1+3是5
>、1+4 为9
、1+5 为17
您可以为要检查的每种情况创建数组。
至少,我希望这能给你一些想法或提示。
问候,
You can use binary numbers for each checkbox:
=2
0
)=2
1
)=2
2
)=2
2
)=2
4
)Hence, when you sum each permutation, you have a unique number you can check.
0
, 2 only is1
and so on3
, 1+3 is5
, 1+4 is9
, 1+5 is17
You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.
Regards,
@JMax 所指的通常称为位屏蔽。这是一个快速教程:
创建一个名为 Form1 的示例表单,其中包含 5 个名为 Check1、Check2、Check3、Check4、Check5 的复选框。然后在标准代码模块中添加以下两个函数:
现在打开 Form1 并选中复选框 1、3 和 4。运行 ShowSelectedBoxes 例程,您应该在立即窗口中得到以下输出:
我使用了
For.. .Loop
是为了在我的示例中实现紧凑性,但您也可以轻松地将其分解为单独的If...Then
语句,以对选中的框执行更有意义的操作。此方法将支持最多 31 或 32 个单独的复选框。
What @JMax is referring to is more commonly known as bit-masking. Here's a quick tutorial:
Create a sample form named Form1 with 5 checkboxes named Check1, Check2, Check3, Check4, Check5. Then add the following two functions in a standard code module:
Now open Form1 and check boxes 1, 3, and 4. Run the ShowSelectedBoxes routine and you should get the following output in the immediate window:
I used the
For...Loop
for compactness in my sample, but you can just as easily break it out into separateIf...Then
statements to do something more meaningful with the checked boxes.This approach will support up to 31 or 32 separate checkboxes.