Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Program an "if statement" in vba

1

I failed recently to program an if statement if vba.

dim IfStatement as string
IfStatement= "(activecell.value=200900)"
IfStatement=IfStatement & "or (activecell.value=7206)"
if IfStatement then
 ' Do this and do that
end if

It could be that I just failed in the building of the IfStatement.  My question is: Can one build an if statement for use in vba?

Thanks muchly

Michael

Post Edited
CODE Tags: You must add [CODE][/CODE] tags around your code! (click the CODE button to do this when creating a post)
Answer
Discuss

Discussion

Thank you.

What I am trying to do is this.

There is a constantly changing list in Excel for those customers that are "different" in some way.

So I could use Search with this list against the Big List to find those customers and treat them differently.

Or I could build an "if" statement and add each one of them to the "if" until I have them all. Then I evaluate each customer with the "if" I built.

You seem to say I can't do it because if conditions are not really "text" but variable.

You are probably right.  I tried very hard to actually build an "if" statement and it didn't work like a real "if" statement.

Thanks again.

Michael
MichaelU (rep: 4) May 12, '17 at 2:34 am
You can use the method that I describe to pull-in a value to the IF statement's condition or put that value into a variable like this: yourVariable = ActiveCell.Value and then reference yourVariable in the IF statement.

In your example, you put ActiveCell.Value inside of quotes "(activecell.value=200900)", which makes that text, so it is not executed and no value is brought in from the spreadsheet.
don (rep: 1989) May 15, '17 at 7:24 am
Thank you so much.

It should have been "(activecell.value=" & CompanyCode & ")". 
Then the code is If-able.

Great.

Michael
MichaelU (rep: 4) May 15, '17 at 8:25 am
Add to Discussion

Answers

0
Selected Answer

Yes you can for sure use IF statements in VBA; this is one of the cornerstones of making more complex macros. It looks like you are a little confused about how they work.

Here is the basic syntax:

If condition Then
    'Do something
End If

condition must evaluate to True or False

Here is our tutorial on IF statements in VBA and that should help you better understand how to use them.

I'm not sure what you are trying to do with this variable: IfStatement

In your example, you use only text in the condition and you can't evaluate straight-up text in the way that you are trying to do it.

You should put any values that you want to compare into separate variables and then compare them within the IF statement's condition or just reference the values directly within the condition.

So, maybe you are trying to do something like this:

If ActiveCell.Value = 200900 Or ActiveCell.Value = 7206 Then

    MsgBox "Hi"

End If
Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login