Msgbox entry validation

D

daniel chen

The following Macro will not loop more than twice the error entries.
Will someone help me correct it to loop indefinely? Thanks

Sub Entry_validation()
Dim Teststring As String
Dim Inputstring As String

Cells(1, 1) = "ABC" 'as given filename
Cells(2, 1) = "DEF" 'as given filename
Cells(3, 1) = "GHI" 'as given filename
Cells(4, 1) = "JKL" 'as given filename

errorloop:
Inputstring = InputBox(prompt:="Enter Filename" & vbLf & "Enter abort
to abort entry" & "")
If Inputstring = "abort" Then GoTo eof
On Error GoTo errornote
Teststring = Application.Match(Inputstring, Range("A1:A10"), 0)

MsgBox "A valid entry."

GoTo eof
errornote:
MsgBox "Not a valid entry." & vbLf & "Try again"
GoTo errorloop
eof:
End Sub
 
N

Norman Jones

Hi David,

Try:

Sub Entry_validation()
Dim Inputstring As String
Dim boolValidEntry As Boolean

Cells(1, 1) = "ABC" 'as given filename
Cells(2, 1) = "DEF" 'as given filename
Cells(3, 1) = "GHI" 'as given filename
Cells(4, 1) = "JKL" 'as given filename
errorloop:
Do While boolValidEntry = False
Inputstring = InputBox(prompt:="Enter Filename" _
& vbLf & "Enter abort to abort entry" & "")
If LCase(Inputstring) = "abort" Then Exit Sub
If Not IsError(Application.Match _
(Inputstring, Range("A1:A10"), 0)) Then
GoTo eof
Else
MsgBox "Not a valid entry." & vbLf & "Try again"
GoTo errorloop
End If
Loop

errornote:
MsgBox "Not a valid entry." & vbLf & "Try again"
GoTo errorloop
eof:
MsgBox "A valid entry."
boolValidEntry = True
End Sub
 
N

Norman Jones

Hi David,

I omitted to delete redundant bits of legacy code.

Replace the code with:

Sub Entry_validation()
Dim Inputstring As String
Dim boolValidEntry As Boolean

Cells(1, 1) = "ABC" 'as given filename
Cells(2, 1) = "DEF" 'as given filename
Cells(3, 1) = "GHI" 'as given filename
Cells(4, 1) = "JKL" 'as given filename

Do While boolValidEntry = False
Inputstring = InputBox(prompt:="Enter Filename" _
& vbLf & "Enter abort to abort entry" & "")
If LCase(Inputstring) = "abort" Then Exit Sub
If Not IsError(Application.Match _
(Inputstring, Range("A1:A10"), 0)) Then
GoTo eof
Else
MsgBox "Not a valid entry." & vbLf & "Try again"
End If
Loop

eof:
MsgBox "A valid entry."
boolValidEntry = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top