Validation 验证输入框的输入

Validation 验证输入框的输入,validation,excel,inputbox,vba,Validation,Excel,Inputbox,Vba,我试图得到一个输入框来验证用户将要输入的内容 我使用下面的脚本,但无法得到验证工作,任何帮助将不胜感激 Sub inputbox() Dim Manager As Long On Error Resume Next Application.DisplayAlerts = False Manager = Application.inputbox(Prompt:="Please enter a manager.", Title:="Pick A

我试图得到一个输入框来验证用户将要输入的内容

我使用下面的脚本,但无法得到验证工作,任何帮助将不胜感激

 Sub inputbox()

 Dim Manager As Long

     On Error Resume Next

        Application.DisplayAlerts = False

        Manager = Application.inputbox(Prompt:="Please enter a manager.", Title:="Pick A Manager Name",      Type:=1)

On Error GoTo 0

Application.DisplayAlerts = True

    If Manager = "" Then

        Exit Sub

    ElseIf Manager <> Ben, Cameron, Chris, Martin, Peter Then

    MsgBox "Incorrect Name, pick a new one!"

    Else

        MsgBox "Your input was " & Manager

    End If

 End Sub

我还没有在Excel中使用InputBox,但我想它与Access非常相似。我使用以下方法验证inputbox:

Dim strM as string

EnterManager:
strM = InputBox("Enter Manager.")
If StrPtr(strM) = 0 Then 'Cancel was pressed
    ' Handle what to do if cancel pressed
    Exit Sub
ElseIf Len(strM) = 0 Then 'OK was pressed with nothing entered
    MsgBox "You must enter a Manager."
    GoTo EnterBuyer
End If
要添加您的标准,您可以添加另一个,如果,我不确定您是否可以使用检查姓名列表的方法。我也不明白你是如何将一个长经理与一个名字列表Ben、Cameron、Chris、Martin、Peter进行比较的,除非他们被分配了变量,在这种情况下,我建议添加前缀,这样就更明显了,比如lBen而不是strBen,这样你就可以很容易地看到变量类型的差异

If strM <> "Ben" And strM <> "Cameron" And strM <> "Chris" And strM <> _
    "Martin" And strM <> "Peter" Then
    MsgBox "Incorrect Name, pick a new one!"
Else
    MsgBox "Your input was " & strM
End If

我还没有在Excel中使用InputBox,但我想它与Access非常相似。我使用以下方法验证inputbox:

Dim strM as string

EnterManager:
strM = InputBox("Enter Manager.")
If StrPtr(strM) = 0 Then 'Cancel was pressed
    ' Handle what to do if cancel pressed
    Exit Sub
ElseIf Len(strM) = 0 Then 'OK was pressed with nothing entered
    MsgBox "You must enter a Manager."
    GoTo EnterBuyer
End If
要添加您的标准,您可以添加另一个,如果,我不确定您是否可以使用检查姓名列表的方法。我也不明白你是如何将一个长经理与一个名字列表Ben、Cameron、Chris、Martin、Peter进行比较的,除非他们被分配了变量,在这种情况下,我建议添加前缀,这样就更明显了,比如lBen而不是strBen,这样你就可以很容易地看到变量类型的差异

If strM <> "Ben" And strM <> "Cameron" And strM <> "Chris" And strM <> _
    "Martin" And strM <> "Peter" Then
    MsgBox "Incorrect Name, pick a new one!"
Else
    MsgBox "Your input was " & strM
End If

虽然不建议使用与内置名称相同的子名称,但您可以按照下面的要求执行操作

首先,您需要将InputBox类型更改为2字符串,因为您正在与字符串进行比较。然后,您应该创建一个函数来检查输入是否是管理器列表的一部分

Sub inputbox()
    On Error Resume Next
    Dim Manager As String

    Manager = Application.inputbox(Prompt:="Please enter a manager name:", Title:="Pick A Manager Name", Type:=2)

    If Manager <> "" Then
        If IsManager(Manager) Then
            MsgBox "Your input was " & Manager
        Else
            MsgBox "Incorrect Name, pick a new one!"
        End If
    End If
End Sub

Private Function IsManager(sTxt As String) As Boolean
    Dim aManagers As Variant, oItem As Variant, bAns As Boolean
    aManagers = Array("Ben", "Cameron", "Chris", "Martin", "Peter")
    bAns = False
    For Each oItem In aManagers
        If LCase(oItem) = LCase(Trim(sTxt)) Then
            bAns = True
            Exit For
        End If
    Next
    IsManager = bAns
End Function

虽然不建议使用与内置名称相同的子名称,但您可以按照下面的要求执行操作

首先,您需要将InputBox类型更改为2字符串,因为您正在与字符串进行比较。然后,您应该创建一个函数来检查输入是否是管理器列表的一部分

Sub inputbox()
    On Error Resume Next
    Dim Manager As String

    Manager = Application.inputbox(Prompt:="Please enter a manager name:", Title:="Pick A Manager Name", Type:=2)

    If Manager <> "" Then
        If IsManager(Manager) Then
            MsgBox "Your input was " & Manager
        Else
            MsgBox "Incorrect Name, pick a new one!"
        End If
    End If
End Sub

Private Function IsManager(sTxt As String) As Boolean
    Dim aManagers As Variant, oItem As Variant, bAns As Boolean
    aManagers = Array("Ben", "Cameron", "Chris", "Martin", "Peter")
    bAns = False
    For Each oItem In aManagers
        If LCase(oItem) = LCase(Trim(sTxt)) Then
            bAns = True
            Exit For
        End If
    Next
    IsManager = bAns
End Function

OK看起来不错,那么如何将strM的值输入到单元格G9中名为PID的表中。谢谢。OK看起来不错,那么如何将strM的值输入到单元格G9中名为PID的表格中。谢谢。我喜欢数组的使用,我应该抓到那个。如果IsErrorApplication.matchTrimsText,aManagers,False,则如果条目不在数组中,则返回true,而不是在数组中循环。谢谢提示!对其他读者来说:应该是Application.WorksheetFunction.match。就像使用数组一样,我应该捕捉到它。如果IsErrorApplication.matchTrimsText,aManagers,False,则如果条目不在数组中,则返回true,而不是在数组中循环。谢谢提示!对于其他读者:它应该是Application.WorksheetFunction.Match