(Excel Userform)检查Userform中的所有复选框是否被选中 [英] (Excel Userform) Check if all checkboxes in a Userform are checked

查看:174
本文介绍了(Excel Userform)检查Userform中的所有复选框是否被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从来没有尝试过UserForm复选框,所以我甚至不知道如何指向用户窗体中的复选框。



这是我现在所拥有的。我知道,我知道这是完全错误的。请帮助?

  Private Sub Step1_Confirm_Click()
Dim i As Byte
Dim Done As Boolean

对于i = 1到4
如果Step1_(i).value = True然后
Done = True
End If
Next i

如果未完成= True然后
MsgBox请确保您已经完成所有
结束If

End Sub

基本上我有:


  1. IOSP_Acc_R_Approval_Step1


  2. 4个复选框称为 Step1_1 Step1_2 ; Step1_3 ; Step1_4


  3. 一个名为的按钮Step1_Confirm / p>


  4. 我希望按钮显示错误,如果不是全部复选框被选中 - 这意味着所有复选框必须被检查....(如果我的英文是太糟糕了,表达我的意思)



解决方案




  • 假设所有复选框都通过将标志设置为 True

  • 迭代复选框,如果没有选中,将标志设置为 False 并退出

  • 结束循环,如果所有复选框都被选中,那么标志仍然是 True



您可以使用 Me.Controls 集合动态地引用复选框,并传入复选框的名称,如Step1_&我



示例代码:

  Option Explicit 

Private Sub Step1_Confirm_Click()

Dim i As Long'< - use Long,not Byte
Dim blnResult As Boolean

'设置一个标志,假设所有复选框都被检查是真的
blnResult = True

'获取每个复选框的值
对于i = 1到4
如果Me.Controls(Step1_& i).Value = False然后
blnResult = False
退出对于'& - - 跳过循环,如果至少有一个框未检查
End If
Next i

'检查标志的值
如果blnResult = False然后
MsgBox请确保你已经完成了
Else
'所有框都被检查...
MsgBox全部检查
如果

End Sub


Never tried UserForm checkboxes before so I don't even know how to point to the Checkboxes in a Userform.

This is what I have at the moment....and I know, I know, it is completely wrong. Please help?

Private Sub Step1_Confirm_Click()
    Dim i As Byte
    Dim Done As Boolean

    For i = 1 To 4
        If Step1_(i).value = True Then
            Done = True
        End If
    Next i

    If Not Done = True Then
        MsgBox "Please make sure you have done all"
    End If

End Sub

Basically I have:

  1. A Userform called IOSP_Acc_R_Approval_Step1

  2. 4 checkboxes called Step1_1; Step1_2; Step1_3; Step1_4

  3. A button called Step1_Confirm

  4. I want the button to show Error, if not all checkboxes are checked - meaning that all checkboxes have to be checked....(in case my English is too bad to convey my meaning)

解决方案

You can do this by:

  • assume that all checkboxes are checked by setting a flag to True
  • iterate the checkboxes and if one is not checked set the flag to False and exit
  • at the end of the loop, if all checkboxes were checked then the flag is still True

You can refer to the checkboxes dynamically by using the Me.Controls collection and pass in the name of the checkbox like "Step1_" & i.

Example code:

Option Explicit

Private Sub Step1_Confirm_Click()

    Dim i As Long '<-- use Long, not Byte
    Dim blnResult As Boolean

    ' set a flag to assume that it is true that all checkboxes are checked
    blnResult = True

    ' get the value of each check box
    For i = 1 To 4
        If Me.Controls("Step1_" & i).Value = False Then
            blnResult = False
            Exit For '<-- skip loop if at least one box not checked
        End If
    Next i

    ' check the value of the flag
    If blnResult = False Then
        MsgBox "Please make sure you have done all"
    Else
        ' all boxes checked ...
        MsgBox "All checked"
    End If

End Sub

这篇关于(Excel Userform)检查Userform中的所有复选框是否被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆