How to use an 'Or Operator'

C

corky_guy

Hi -
I'm trying to write a simple bit of code that simply checks for proper
formatting of text. The problem is that the code I have used does not
seem to properly work. Can someone offer a suggestion of how to fix?

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If (Not Left(.Text, 3) Like "so-") Or (Not Left(.Text, 3) Like
"t3-") Then
MsgBox "so-x/x/x or t3-x/x/x"
Cancel = True
End If
End With
End Sub


Basically, the inputted text MUST BE either: so-x/x/x or t3-x/x/x --
it cannot be anything other than that.

Thank you for any help you can provide.
 
G

Greg Maxey

corky_guy

Look at your logic:

My house is white

If my house isn't white OR my house isn't black Then
Msgbox "Since your house isn't black you see this message"
End If

Now consider:

My house is white

If my house isn't white AND my house isn't black Then
Msgbox "Since your house is white And it isn't black you don't see this
message"
Else
Msgbox "Since your house is black or white you see this message"
End If

and this:


My house is blue

If my house isn't white AND my house isn't black Then
Msgbox "Since your house isn't white And it isn't black your must be some
other color"
Else
Msgbox "Since your house is black or white you see this message"
End If

Try:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
MsgBox Left(.Text, 3)
If Not Left(.Text, 3) Like "so-" And Not Left(.Text, 3) Like "t3-" Then
MsgBox "so-x/x/x or t3-x/x/x"
Cancel = True
End If
End With
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