Search code examples
vbaexcelloopsinputboxdo-loops

VBA Do Loop and Input Box content not working


I'm new with VBA loops. What I'm trying to do is a do loop and input box procedure that asks for a product code until a valid one is input. The code should start with the letter P, and should be followed by four digits. For an invalid code input, it should display a message telling the user why it's invalid.

I have the following procedure coded. It works for when the user types p9887 for example.

However, if the user types o899876 or p877789, it gives the message "Product Code should have five characters" and then the user has to input again. In this second try, if user types p9876, while it meets all the criteria, the message that comes up from my procedure is "The last four characters should be digits" and it is stuck at that loop where the user has to type in their input again, and the same message comes up.

Any insight on what I'm doing wrong is greatly appreciated!

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

************************ Here is another type of code that is cleaner, but still does the same issue.

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

Solution

  • Try this

    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
    

    A more "helping" input block code could be the following:

    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