Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Syntax Error If string variable equals "a" or "b" or "c"

I'm trying to write code that does the following in VBA but i get error: Type mismatch

Quark is a string variable

 If quark = "F" Or "DE" Or "ED" Then
like image 553
PocketLoan Avatar asked Sep 17 '13 20:09

PocketLoan


People also ask

How do I fix syntax error in VBA?

To do this, click on 'Tools' and then click on 'Options'. In the options dialog box, make sure that the 'Auto Syntax Check' option is enabled. If the 'Auto Syntax Check' option is disabled, VBA will still highlight the line with the syntax error in red, but it will not show the error dialog box.

How do you check if a string is equal to another string in VBA?

Excel VBA String Comparison. We have a built-in function to compare two strings in VBA: “StrComp.” We can read it as “String Comparison.” This function is available only with VBA and not as a Worksheet function. It compares any two strings and returns the results as “Zero (0)” if both strings match.

How do you use multiple conditions in if statement Excel VBA?

You can use the OR operator with the VBA IF statement to test multiple conditions. When you use it, it allows you to test two or more conditions simultaneously and returns true if any of those conditions are true. But if all the conditions are false only then it returns false in the result.

What are the 3 different types of error handling techniques in VBA?

AutoCAD to Excel - VBA Programming Hands-On! There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.


2 Answers

Each "OR" is its own boolean statement.

If quark = "F" Or quark ="DE" Or quark ="ED" Then

You could acheive what you are trying to do with a case though I think

Select Case quark 
Case "F","DE","ED" 
     stuffHere
end select

EDIT: I see that you were getting a type mismatch, did you dim quark as a string?

dim quark as string
like image 123
Elias Avatar answered Oct 18 '22 07:10

Elias


You may try like to compare like this:-

If quark = "F" Or quark = "DE" Or quark = "ED" Then

instead of:-

If quark = "F" Or "DE" Or "ED" Then
like image 43
Rahul Tripathi Avatar answered Oct 18 '22 08:10

Rahul Tripathi