检查两种可能性时出错

时间:2016-06-23 13:22:07

标签: vba excel-vba excel

我当前的代码检查lengthListbox或lengthListbox2是否有选择,否则它将显示错误消息。最简单的方法是什么,以便从lengthListbox或lengthListbox2中选择,或者显示消息“请输入项目长度”?

Function CheckInputs() As Boolean
    If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
    If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
    If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
    If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
    If Not CheckControl(Me.lengthListbox, "Please select a current year month") Then Exit Function
    If Not CheckControl(Me.lengthListbox2, "Please select a next year month") Then Exit Function

    CheckInputs = True
End Function

Private Function CountSelectedListBoxItems(lb As MSForms.ListBox) As Long
    Dim i As Long
    With lb
        For i = 0 To .ListCount - 1
            If .Selected(i) Then CountSelectedListBoxItems = CountSelectedListBoxItems + 1
        Next i
    End With
End Function

Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
    Select Case TypeName(ctrl)
        Case "TextBox"
            CheckControl = Trim(ctrl.Value) <> ""
        Case "ComboBox"
            CheckControl = ctrl.ListIndex <> -1
        Case "ListBox"
            CheckControl = CountSelectedListBoxItems(ctrl) > 0
'        Case Else
    End Select
    If CheckControl Then Exit Function
    ctrl.SetFocus
    MsgBox errMsg
End Function

我尝试过的一些事情:

尝试在输入按钮中输入错误检查

Private Sub enterButton_Click()
    If Me.lengthListbox.ListIndex = -1 And Me.lengthListbox2.ListIndex = -1 Then
    MsgBox "Please enter Project Length"
    End If
    If Not CheckInputs Then Exit Sub 'check for fields to have values
    Process GetWs(Me.impactCombobox.Value) ' process data passing the proper worksheet got from GetWs() function
    MsgBox "Project Entered Successfully"
    ClearUFData 'clear the data
End Sub

我还试过制作一个单独的子功能,仅用于检查列表框,但无法使其在CheckInputs中工作

1 个答案:

答案 0 :(得分:0)

尝试两个功能稍加修改:

1)嵌套列表框检查(参见CheckInputs = True之前的最后一行)

Function CheckInputs() As Boolean
    If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
    If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
    If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
    If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
    If Not CheckControl(Me.lengthListbox, "") Then If Not CheckControl(Me.lengthListbox2, "Please enter project length") Then Exit Function

    CheckInputs = True
End Function

这样第一个列表框可能的否定检查将有第二个列表框检查决定&#34;退出&#34;

2)稍微修改CheckControl(见End Select之后的第一行)

Function CheckControl(ctrl As msforms.Control, errMsg As String) As Boolean
    Select Case TypeName(ctrl)
        Case "TextBox"
            CheckControl = Trim(ctrl.Value) <> ""
        Case "ComboBox"
            CheckControl = ctrl.ListIndex <> -1
        Case "ListBox"
            CheckControl = CountSelectedListBoxItems(ctrl) > 0
'        Case Else
    End Select
    If errMsg = "" Then Exit Function
    If CheckControl Then Exit Function
    ctrl.SetFocus
    MsgBox errMsg
End Function

如果传递的错误消息字符串为void

,则不执行任何操作