Link to home
Create AccountLog in
Avatar of Rayne
RayneFlag for United States of America

asked on

Validate User Entry VBA Excel

Hello Everyone,
Validate user input and if the input is not one of the three choices in the code, exit the sub. That’s what I am trying to do but the code is not working smooth. Can anymone assist?

Thank You
samplerIssue.xlsm
Avatar of Rayne
Rayne
Flag of United States of America image

ASKER

Sub Button1_Click()



    Dim entry As String

    Dim validStr1, validStr2, ValidStr3 As String

    validStr1 = "superhero"
    validStr2 = "superee"
    ValidStr3 = "helloworld"

    entry = InputBox("fdsf", "dsfsdf")
    
    
    entry = LCase(entry)

    'if entry is not one of these vlaid choices, then exit sub
 
    
    If entry <> validStr1 Or entry <> validStr2 Or entry Or ValidStr3 Then
        MsgBox "Sorry - " & entry & " is a invalid entry, No Can Do :("
        Exit Sub
    Else
        MsgBox "Correct"

        'Its exiting sub even if "HelloWorld" is entered....whats wrong??
        'As long as the entry is either  "SuperHero" or "SuperEE" or "HelloWorld", the sub should Not Exit....
    End If

End Sub

Open in new window

Avatar of Rayne

ASKER

Also I am getting a Item Mismatch error as well...dont know why :(
SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Rayne

ASKER

Thank you All :)
Respect
Note that apart from the errors in the If line,  if the type is important, you need to specify the type of each variable in a Dim line with several variables. If unspecified the variables will be of variant type