VBA 循环和输入框内容不起作用

VBA Do Loop and Input Box content not working

我是 VBA 循环的新手。我正在尝试做的是一个 do 循环和输入框过程,该过程要求输入产品代码,直到输入有效代码。该代码应以字母 P 开头,后跟四位数字。对于无效的代码输入,它应该显示一条消息,告诉用户它为什么无效。

我编写了以下程序。例如,它适用于用户键入 p9887 时的情况。

但是,如果用户键入 o899876p877789,它会给出消息 "Product Code should have five characters" 然后用户必须再次输入。在第二次尝试中,如果用户键入 p9876,虽然它满足所有条件,但我的过程中出现的消息是 "The last four characters should be digits" 并且它停留在用户必须再次输入其输入的循环中,并且出现相同的消息。

非常感谢任何对我做错的见解!

Option Explicit
Public Sub ProductCode()


Dim strInput As String

Dim intFrstLetter As Integer

Dim intLastFour As String

Dim strFrstLetter As String

Dim test As String

Dim blDone As Boolean


strInput = InputBox("Please enter product code")

intFrstLetter = InStr(1, strInput, "p")

intLastFour = Right(strInput, 4)

strFrstLetter = Left(strInput, 1)

Do

If strFrstLetter = "p" Then

    If Len(strInput) <> 5 Then
      MsgBox "Product code should have five characters."
     strInput = InputBox("Please enter product code")
    Else
       If IsNumeric(intLastFour) Then
       MsgBox "Thank You"
       blDone = True
       Exit Do
       Else
         MsgBox "The last four characters should be digits"
         strInput = InputBox("Please enter product code")
  If strFrstLetter <> "p" Then
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
        End If
    End If
  End If
End If
Loop Until blDone = True
End Sub

*************************** 这是另一种更清晰的代码,但仍然会出现同样的问题。

Public Sub ProductCode()
Dim strInput As String
Dim intFrstLetter As Integer
Dim intLastFour As String
Dim strFrstLetter As String
Dim blDone As Boolean


strInput = InputBox("Please enter product code")

intFrstLetter = InStr(1, strInput, "p")
intLastFour = Right(strInput, 4)
strFrstLetter = Left(strInput, 1)
Do
    If strFrstLetter = "p" Then
        If Len(strInput) = 5 Then
           If IsNumeric(intLastFour) = True Then
            MsgBox "Thank You"
            Exit Do
           Else
            MsgBox "The last four characters should be digits"
            strInput = InputBox("Please enter product code")
           End If
       Else
         MsgBox "Product code should have five characters"
         strInput = InputBox("Please enter product code")
       End If
  Else
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
 End If
Loop

在出错后输入新的产品代码不会更改 4 个变量中的 3 个。将行 strInput = InputBox("Please enter product code") 更改为 call ProductCode。这样你的变量就会根据新的输入而改变。

要自己找出这些错误,您可以使用 "debug" 菜单中的 "Step into",并在浏览代码时将鼠标悬停在变量上。或者,在程序的设计阶段,在要验证的一段代码之后显示变量。然后在确定可以正常工作时删除这些不需要的行。

试试这个

Do While Not blDone
    blDone = InputBox("Please enter product code") Like "P####"
    If Not blDone Then MsgBox "the input didn't match the pattern 'P####' where:" _
                              & vbCrLf & vbCrLf & vbTab & "'P' must be the 'P' letter" _
                              & vbCrLf & vbTab & "'####' must be four integer digits"
Loop

更多"helping"输入块代码如下:

Dim strInput As String, msgStrng As String, defStrng As String
Dim blDone As Boolean

defStrng = "P####  [enter digits for each '#']"
Do While Not blDone
    strInput = InputBox("Please enter product code", "Product Code input", defStrng)
    blDone = strInput Like "P####"
    If Not blDone Then
        Select Case True
            Case Len(strInput) <> 5
                msgStrng = "Product code should have five characters"
                defStrng = Left(strInput, 5)
            Case Left(strInput, 1) <> "P"
                msgStrng = "Product code should start with letter 'P'"
                defStrng = "P" & Left(strInput, 4)
            Case Else
                msgStrng = "last four characters of Product code should be digits"
                defStrng = strInput
            End Select

            MsgBox msgStrng, vbCritical
    Else
        MsgBox "Thank you"
    End If
Loop

我发现了问题并通过以下方式修复了它:

strInput = InputBox("Please enter product code")

Do
If Left(strInput, 1) = "p" Then
    If Len(strInput) = 5 Then
        If IsNumeric(Right(strInput, 4)) = True Then
            MsgBox "Thank You!"
            blDone = True
            Exit Do
        Else
            MsgBox "The last four characters should be digits"
            strInput = InputBox("Please enter product code")
        End If
    Else
        MsgBox "Product code should have five characters"
        strInput = InputBox("Please enter product code")
    End If
Else
    MsgBox "Product code should start with the letter P"
    strInput = InputBox("Please enter product code")
End If

Loop Until blDone = True

问题是声明了这些变量并像这样设置它们并在代码中使用。

intFrstLetter = InStr(1, strInput, "p")
intLastFour = Right(strInput, 4)
strFrstLetter = Left(strInput, 1)

从代码中删除这些变量后,循环就可以正常工作了! 谢谢大家的回复!