首页 > 解决方案 > 如果未选择框架中的选项按钮,如何突出显示框架?

问题描述

我有这段代码来突出显示用户窗体中未填写或未选择的任何可见控件。它可以工作,但即使选择了两个选项按钮中的一个,它仍然会突出显示帧。

Dim ControlsOfInterest As Collection

找到了一个函数来识别框架内的 opt 按钮值:

Function OptionGroupSelectionMade(oneButton As MSForms.OptionButton) As Boolean
    Dim oneControl As MSForms.Control

    For Each oneControl In Me.Controls
        If TypeName(oneControl) = "OptionButton" Then
            If oneControl.Parent.Name = oneButton.Parent.Name Then
                If oneButton.GroupName = oneControl.GroupName Then
                    If oneControl.Value Then
                        OptionGroupSelectionMade = True
                        Exit For
                    End If
                End If
            End If
        End If
    Next oneControl

End Function

然后其余的代码应该只在它们可见时识别未输入的值

'在初始化

Dim oneControl As MSForms.Control
Dim onePage As MSForms.Page

Set ControlsOfInterest = New Collection
For Each onePage In Me.MultiPage1.Pages
    If cmbFundingCategory.Value <> "Req Reduction" Or cmbFundingCategory.Value = "Combo (Unreq'd Dollars + Req Reduction)" Then
    Else
    For Each oneControl In onePage.Controls
        If oneControl.Visible And oneControl.Parent.Visible Then
            If oneControl.Name Like "txt*" And oneControl.Visible Then
                ControlsOfInterest.Add item:=oneControl, key:=oneControl.Name
            ElseIf oneControl.Name Like "opt*" And oneControl.Visible Then
                ControlsOfInterest.Add item:=oneControl, key:=oneControl.Name
            ElseIf oneControl.Name Like "cmb*" And oneControl.Visible Then
                ControlsOfInterest.Add item:=oneControl, key:=oneControl.Name
            End If
        End If
    Next oneControl
End If
Next onePage

遍历帧以检查每个帧以查看选项按钮是否为真?

Private Sub CommandButton1_Click()
    Dim colBlankFields As New Collection
    Dim oneControl As MSForms.Control
    Dim i As Long
    Dim strPrompt As String

    For Each oneControl In ControlsOfInterest
        oneControl.BackColor = vbWhite
        If oneControl.Name Like "opt*" Then
            If Not OptionGroupSelectionMade(oneControl) Then
                oneControl.BackColor = RGB(255, 128, 128)
                colBlankFields.Add item:=oneControl, key:=oneControl.Name
                strPrompt = strPrompt & vbCr & oneControl.Name & " on page " & oneControl.Parent.Caption
             End If
        Else
        If oneControl.Visible And oneControl.Text = vbNullString Then
                oneControl.BackColor = RGB(255, 128, 128)
                colBlankFields.Add item:=oneControl, key:=oneControl.Name
                strPrompt = strPrompt & vbCr & oneControl.Name & " on page " & oneControl.Parent.Caption
            End If
        End If
    Next oneControl
    If colBlankFields.Count <> 0 Then
        MsgBox "some controls need values" & vbCr & strPrompt
    End If
End Sub

但我仍然在 msgbox 中填充了隐藏字段的值,直到组合框“cmbFundingCategory”应该显示它们

标签: excelvba

解决方案


推荐阅读