【问题标题】:VBA check if all userform comboboxes have an option selectedVBA检查是否所有用户表单组合框都选择了一个选项
【发布时间】:2021-03-22 18:13:42
【问题描述】:

我有一个带有许多组合框和一个“确定”按钮的用户表单。我需要的是,当按下“确定”按钮时,VBA 检查是否没有空的组合框。如果所有组合框都选择了某个值 - 关闭用户表单,否则返回一个消息框并单击该消息框上的“确定”返回用户表单,没有丢失填充值。

我已经尝试了所有我能想到的方法:

If PackageOuterRadius = null Then

if PackageOuterRadius is nothing Then

 If PackageOuterRadius.value = 0 Then

 If IsNull(PackageOuterRadius) = True Then

 If IsNull(PackageOuterRadius.value) Then

我一直在尝试做的是:

Private Sub Rigid_Filme_Ok_Button_Click()

If PackageOuterRadius.ListCount = 0 Then
MsgBox "Select a ""Package Outer Radius!"
End If

并且绝对没有任何实际检查组合框是否为空并不断返回正值(选择了一个值)

有什么办法可以解决这个问题?有人可以帮帮我吗?

【问题讨论】:

    标签: vba combobox


    【解决方案1】:

    如果您只有几个 ComboBoxes,您可以在 OK 按钮下方插入行以检查是否所有 ComboBoxes 都已填充,但如果您在 UserForm 上有太多 ComboBoxes,您可以借助类模块来实现这一点并做所以,请按照以下步骤...

    1. 插入一个 Class Module 并将其重命名为 clsUserForm 并将以下代码放在 Class Module 上...

    类模块代码:

    Public WithEvents mCMB As msforms.ComboBox
    
    
    Public Sub CheckComboBoxes()
    Dim cCtl    As msforms.Control
    Dim cntCBX  As Long
    Dim cnt     As Long
    
    For Each cCtl In frmMyUserForm.Controls
        If TypeName(cCtl) = "ComboBox" Then
            cntCBX = cntCBX + 1
            If cCtl.Value <> "" Then
                cnt = cnt + 1
            End If
        End If
    Next cCtl
    If cnt = cntCBX Then
        Unload frmMyUserForm
    Else
        MsgBox "All the ComboBoxes are mandatory.", vbExclamation
    End If
    
    End Sub
    
    1. 然后将以下代码放在用户窗体模块上。代码假定 userForm 的名称是 frmMyUserForm,CommandButton 的名称是 cmdOK

    用户窗体模块代码:

    Dim GroupCBX() As New clsUserForm
    Dim frm As New clsUserForm
    
    Private Sub cmdOK_Click()
    frm.CheckComboBoxes
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim i As Long
    Dim ctl As Control
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "ComboBox" Then
            i = i + 1
            ReDim Preserve GroupCBX(1 To i)
            Set GroupCBX(i).mCMB = ctl
        End If
    Next ctl
    
    End Sub
    

    你会很高兴的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多