Excel VBA: Using OR with <>

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm wondering if it's possible to use the OR statement with Not Equal To (<>) in VBA.

I'm trying to simplify the user selection from a MsgBox to validate the entries and then Exit Sub if no valid matches were found, Else proceed with running the Macro.

Here's what I'm working with:
Note: I'm using a list of Student ID's, but I just wanted to test with some names to simplify the testing for now…
VBA Code:
Sub GetStudentName()[/FONT]
 
[FONT=Segoe UI]'Get Name
Dim Student As String
   
Student = Application.InputBox( _
"Enter Students Name to assign new books." & vbCrLf & _
"Click Cancel or leave blank to cancel.", "Copy New Books for...", "Enter student name")
   
Student = Application.WorksheetFunction.Proper(Student) 'Convert input to Propercase
   
'Validate Name
If Student <> "John" Or Student <> "Jane" Or cName = vbNullString Then
MsgBox "You selected Cancel, or entered an invalid name."
Exit Sub
Else
MsgBox "You entered " & Student
End If[/FONT]
 
[FONT=Segoe UI]End Sub[/FONT]

If I enter a valid name, or an invalid name, or hit Cancel... all result with an invalid or canceled entry response.

Any help would be greatly appreciated…
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
This condition will always be evaluated as true because any value for Student will either not be "John" or not be "Jane". Change it to AND.

VBA Code:
If (Student <> "John" And Student <> "Jane") Or cName = vbNullString Then
 
Upvote 1
Solution
This condition will always be evaluated as true because any value for Student will either not be "John" or not be "Jane". Change it to AND.

VBA Code:
If (Student <> "John" And Student <> "Jane") Or cName = vbNullString Then
Thanks Cubist,

This shows how fried my brain is right now working with VBA. This makes perfect sense now.

My tests show valid entries are recongnized as valid entries.
Hitting Cancel or entering an invalid name returns the correct response.

Thank you very much! (y)
 
Upvote 0
Sometimes when you have a list like your list of names to exclude, the Select Case statement is a better bet that trying to do it with If statements. It is more easiliy expandable if the list changes.
VBA Code:
Sub GetStudentName()
'Get Name
    Dim Student As String

    With Application
        Student = .Proper(.Trim(.InputBox("Enter Students Name to assign new books." & vbCrLf & _
                                          "Click Cancel or leave blank to cancel.", "Copy New Books for...", "Enter student name")))
    End With

    'Validate Name
    Select Case Student
    Case "John", "Jane", "Paul", "George", "Ringo"    'invalid student
        MsgBox "You entered an invalid name."
        Exit Sub
    Case "False", vbNullString
        MsgBox "You selected Cancel."
        Exit Sub
    End Select

    MsgBox "You entered " & Student
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top