Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Select two text options

Status
Not open for further replies.

olatzcelaya

Programmer
Jan 4, 2008
13
GB
Hi all,

I am writing a code in VBA for excel i want to write in the code that if you select the text of "120FBK" in a combobox or if you select "125L" in the same combobox, the program runs one thing. The code is the following one, but the program tells me that I have a sintaxis error in the line of the "if". Could anybody teel me how it should be written?

Private Sub ComboBox10_Click()
Dim PaperWeight1 As Integer
If ComboBox10.Text = "120FBK" Or "125L" Then


THANK YOU ;-)

OLATZ
 
How about if you write:
Code:
If ComboBox10.Text = "120FBK" Or ComboBox10.Text = "125L" Then
You could even do a CASE statement
Code:
Select Case ComboBox10.Text
Case "120FBK", "125L"
 'your code if selected
Case Else
 'your code if not selected
End Select
Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
You're welcome.

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
olatz,

Or is a logic statement. Look up "logic statements" in Help, the Or Operator. The table showing possible results is worthwhile to understand. Or (and all other comparing logic statements) requires two full expressions to compare.

Code:
If ComboBox10.Text = "120FBK" Or "125L" Then

has only one. ComboBox10.Text = "120FBK" is a full expression. "125L" is not, it is a value.

HQ's code:
Code:
ComboBox10.Text = "120FBK" Or ComboBox10.Text = "125L"
has the required two full expressions. An Or operation performs a logic operation on expressions, not values.

faq219-2884

Gerry
My paintings and sculpture
 
An Or operation performs a logic operation on expressions, not values
Sorry Gerry but I disagree.
An Or operation performs a logic operation on boolean values.
What you name "full expressions" should be read "full expressions evaluating to a boolean value" or "full logical expressions".
 

It is also good to know, especially if you have a lot of values to consider, this logic:
Code:
If InStr("*[blue]120FBK[/blue]*[blue]125L[/blue]*", "*" & ComboBox10.Text & "*") Then
    [green]'do your magic here[/green]
End If
InStr giving you 0 is False, any other value is True

Have fun.

---- Andy
 
If...Then are always Boolean.

PHV, you are correct.

"An Or operation performs a logic operation on expressions, not values." is not quite accurate.

However, I will have to disagree with: "should be read "full expressions evaluating to a boolean value"

ComboBox10.Text = "120FBK" Or ComboBox10.Text = "125L"

Both sides of the Or are expressions. They themselves do not evaluate to a boolean value. The Or operator itself does that, which is why it requires expressions. Expressions can be compared to each other using Boolean logic.. which is exactly what Or does.

That being said, you are of course still correct. An Or statement can indeed be valid using just values. But those values must already be Boolean.
Code:
Dim Bool_A As Boolean
Dim Bool_B As Boolean
Bool_A = False
Bool_B = True

If [b]Bool_A[/b] Or [/b]Bool_B[/b] Then
is a valid Or operation.

Code:
Dim strOne As String
Dim strTwo As String

strOne = "Yadda"
strTwo = "BlahBlah"

If strOne Or strTwo Then
is NOT. The variables are values, but...it returns a mis-match error as...the values themselves are neither Boolean, nor resolvable as Boolean by the Or operation. Type mis-match.
Code:
Dim strOne As String
Dim strTwo As String

strOne = "Yadda"
strTwo = "BlahBlah"

If strOne = "Mexico" Or strTwo = "BlahBlah" Then
will work as the expressions themselves can be compared by the Or operation using Boolean logic.

So unless the values used by Or are already Boolean, you MUST use expressions that can be compared by Boolean logic.

If ComboBox10.Text = "120FBK" Or "125L" can NOT be compared by Boolean logic, because "125L" is not a value OF something. It is not an expression OF something.

Further,
Code:
Dim strOne As String
Dim Bool_A As Boolean

Bool_A = False
strOne = "Yadda"

If strOne Or Bool_A Then[/code[ will also fail the Or, type mis-match.  Even though both sides of the Or ARE values.  StrOne can not be resolved using Boolean logic.  It is the Or operation itself that performs the boolean logic.  The expression themselves do not need to resolve to boolean, it is the [b]comparison[/b] - as performed by Or - that must be resolvable to boolean.

faq219-2884

Gerry
[url=http://www3.telus.net/public/fumei/]My paintings and sculpture[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top