Prevent referencing the same control when using IF in VBA

ChrisNeal1990

Registered User.
Local time
Today, 02:59
Joined
Mar 25, 2017
Messages
11
Hi,

Does anyone know a way I can prevent referencing the same control when using the IF function in VBA.

The below does not work

Private Sub comboboxA_AfterUpdate()
If comboboxA = 1 Or 100 Or 24 Then
MsgBox "sdffds", vbOKOnly, "dfgdsfg"
End If
End Sub

Whereas the below does work

Private Sub comboboxA_AfterUpdate()
If comboboxA = 1 Or comboboxA = 100 Or comboboxA = 24 Then
MsgBox "sdffds", vbOKOnly, "dfgdsfg"
End If
End Sub
 
What do you mean "prevent referencing" it?

To compare to a list of values you can try a Case statement, if that is what you mean....
Code:
Private Sub comboboxA_AfterUpdate()
   Select Case Me.comboboxA
      Case 1, 100, 24
         MsgBox "sdffds", vbOKOnly, "dfgdsfg"
   End Select
End Sub
hth
Mark
 
Where the code works I have had to type in the same control each time.
 
Mark just gave you how you can reference the control ONCE.
 
He is referring to the following, but I've highlighted it:

Code:
Private Sub comboboxA_AfterUpdate()
If [B]comboboxA[/B] = 1 Or [B]comboboxA[/B] = 100 Or [B]comboboxA[/B] = 24 Then
MsgBox "sdffds", vbOKOnly, "dfgdsfg"
End If
End Sub

rather than
Code:
If comboboxA = 1 Or 100 Or 24 Then

I'd second the use of CASE myself.
 
Thank you Mark that is correct in what I meant. Apologies I couldn't think of a way to make it any clearer.

Select CASE works, thanks MarkK
 

Users who are viewing this thread

Back
Top Bottom