VBA 显示选择选项

发布于 2024-11-25 16:39:24 字数 520 浏览 1 评论 0原文

我正在尝试编写一个代码,该代码将根据选中的复选框显示一个值。总共有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-12-02 16:39:24

您可以为每个复选框使用二进制数

  • 第一个值为 1 (=20)
  • 第二个值是 2 (=21)
  • 第三个值是 4 (=22)
  • 第四个值为 8 (=22
  • 第五个值是 16 (=24)

因此,当您对每个排列求和时,您将得到一个唯一的数字可以检查。

  1. 1仅是0,2仅是1,依此类推
  2. 1+2是3,1+3是5 >、1+4 为 9、1+5 为 17
  3. 等等

您可以为要检查的每种情况创建数组。
至少,我希望这能给你一些想法或提示。

问候,

You can use binary numbers for each checkbox:

  • First value is 1 (=20)
  • Second value is 2 (=21)
  • Third value is 4 (=22)
  • Fourth value is 8 (=22)
  • Fifth value is 16 (=24)

Hence, when you sum each permutation, you have a unique number you can check.

  1. 1 only is 0, 2 only is 1 and so on
  2. 1+2 is 3, 1+3 is 5, 1+4 is 9, 1+5 is 17
  3. and so on

You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.

Regards,

邮友 2024-12-02 16:39:24

@JMax 所指的通常称为位屏蔽。这是一个快速教程:

创建一个名为 Form1 的示例表单,其中包含 5 个名为 Check1、Check2、Check3、Check4、Check5 的复选框。然后在标准代码模块中添加以下两个函数:

Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long

    For Each Ctl In Forms!form1
        If Ctl.ControlType = acCheckBox Then
            If Ctl Then
                Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
            End If
        End If
    Next Ctl
    GetSelectedBoxes = Total
End Function

Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
    Total = GetSelectedBoxes
    For i = 1 To 5
        If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
    Next i
End Sub

现在打开 Form1 并选中复选框 1、3 和 4。运行 ShowSelectedBoxes 例程,您应该在立即窗口中得到以下输出:

Check1 selected
Check3 selected
Check4 selected

我使用了 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:

Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long

    For Each Ctl In Forms!form1
        If Ctl.ControlType = acCheckBox Then
            If Ctl Then
                Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
            End If
        End If
    Next Ctl
    GetSelectedBoxes = Total
End Function

Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
    Total = GetSelectedBoxes
    For i = 1 To 5
        If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
    Next i
End Sub

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:

Check1 selected
Check3 selected
Check4 selected

I used the For...Loop for compactness in my sample, but you can just as easily break it out into separate If...Then statements to do something more meaningful with the checked boxes.

This approach will support up to 31 or 32 separate checkboxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文