Avatar of gdunn59
gdunn59 asked on

How come my IF/OR statements are not working.

Excel VBA code to do nested IF/OR statements, not working?


If I remove the 2nd IF statement , it works.

"=IF(LEFT([@2AFSC]])=""10"",1,0)"

Open in new window


Here is my code:

DataTable.ListColumns.Add(5).Name = "IS_COMM"
Range("Pivot_Table[[#Headers],[IS_COMM]]").Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=IF(LEFT([@[DAFSC_CURR]])=""C"",1,0)" Or "=IF(LEFT([@2AFSC]])=""10"",1,0)"

Open in new window


How can I get it to work with the 2nd if statement, also need to add 2-3 more if statements to it.


Thanks!

VBA

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

Try this:

ActiveCell.FormulaR1C1 = "=IF(OR(LEFT([@[DAFSC_CURR]])=""C"",LEFT([@2AFSC]])=""10""),1,0)"

Open in new window

Although I don't see any length specified for the LEFT() function, how many characters did you want to look at?  Maybe this:

ActiveCell.FormulaR1C1 = "=IF(OR(LEFT([@[DAFSC_CURR]],1)=""C"",LEFT([@2AFSC]],2)=""10""),1,0)"

Open in new window


»bp
ASKER
gdunn59

Bill Prew:

I just want to look at the first character in the first IF statement, and the first 2 in the second IF statement.

I will try your suggestion and let you know what happens.

Thanks much!
ASKER
gdunn59

Bill Prew:

Neither of these worked:

ActiveCell.FormulaR1C1 = "=IF(OR(LEFT([@[DAFSC_CURR]])=""C"",LEFT([@2AFSC]])=""10""),1,0)"

ActiveCell.FormulaR1C1 = "=IF(OR(LEFT([@[DAFSC_CURR]],1)=""C"",LEFT([@2AFSC]],2)=""10""),1,0)"

As I mentioned in my initial posting, for some reason it will not work with both IF statements, but the minute I remove the 2nd IF statement, it works.

Thanks!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

It looks like in one of the conditions you have the @ outside the [ and in the other inside, could that be an issue?
ASKER CERTIFIED SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
gdunn59

Bill Prew,

OMG . . . Good eyes.  That was it.  I was missing a [ in the second condition.

Thank you so much!
Bill Prew

Great, glad that was useful.

»bp
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.