# VBA conditions in IF statement

I can't get the below to result in anything but "GR" in column P
In Services module.

If the column C is 9,17,22,23,25,92,96  and column T >=150 value in column P "GR_DEFHW"
If the column C is 9,17,22,23,25,92,96  and column T <150 value in column P "GR_DEF"
If the column C is 51,54  and column T >=150 value in column P "GR_CAHW"
If the column C is 51,54  and column T <150 value in column P "GR_CA"

Can anyone help???

``````'If Column A = GR
If Cells(c, "Y") = "GR" Then
If Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "T") <= 150 Then Cells(c, "P") = "GR_DEF"
If Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "T") > 150 Then Cells(c, "P") = "GR_DEFHW"
If Cells(c, "C") = "51" Or Cells(c, "C") = "54" And <= 150 Then Cells(c, "P") = "GR_CA"
If Cells(c, "C") = "51" Or Cells(c, "C") = "54" And > 150 Then Cells(c, "P") = "GR_CAHW"
If Cells(c, "C") = "51" Or Cells(c, "C") = "54" Then Cells(c, "P") = "GR_CA"
If Cells(c, "T") > 150 Then Cells(c, "P") = "GRHW"
If Cells(c, "T") <= 150 Then Cells(c, "P") = "GR"
``````
Rerate-v5.4-sample.xlsm
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT / Software Engineering ConsultantCommented:
Try this:

``````Select Case Cells(c, "C")
Case 9, 17, 22, 23, 25, 92, 96
If Cells(c, "T") < 150 Then
Cells(c, "P") = "GR_DEF"
Else
Cells(c, "P") = "GR_DEFHW"
End If
Case 51, 54
If Cells(c, "T") < 150 Then
Cells(c, "P") = "GR_CA"
Else
Cells(c, "P") = "GR_CAHW"
End If
Case Else
If Cells(c, "T") < 150 Then
Cells(c, "P") = "GR"
Else
Cells(c, "P") = "GRHW"
End If
End Select
``````
~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Excel & VBA ExpertCommented:
Try this......

``````If Cells(c, "Y") = "GR" Then
If Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "T") <= 150 Then
Cells(c, "P") = "GR_DEF"
ElseIf Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "T") > 150 Then
Cells(c, "P") = "GR_DEF"
ElseIf Cells(c, "C") = "51" Or Cells(c, "C") = "54" Then
Cells(c, "P") = "GR_CA"
ElseIf Cells(c, "T") > 150 Then
Cells(c, "P") = "GRHW"
ElseIf Cells(c, "T") <= 150 Then
Cells(c, "P") = "GR"
End If
End If
``````
Commented:
I started on row 2 with my formula test.  Please place the following formula in P2 and test by changing values in your C and T columns.
``````=IF(ISERROR(SEARCH(","&C2&",",",9,17,22,23,25,92,96,")),IF(ISERROR(SEARCH(","&C2&",",",51,54,")),"CvalueNOTfound",IF(T2>=150,"GR_CAHW","GR_CA")),IF(T2>=150,"GR_DEFHW","GR_DEF"))
``````
Note:"CvalueNOTfound" will be the formula result if the C column value does not match any of those you've specified.  You can change that to "" if you don't want to display anything for the user under that condition.
Author Commented:
Excellent!! Whew, nice.
Excel & VBA ExpertCommented:
Glad to help. :)
Author Commented:
Wait - I am not getting any heavy weights - I know this is closed, but can you take a look? Should I re-post??

``````'If Column A = GR
If Cells(c, "A") = "GR" Then
If Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "S") <= 150 Then
Cells(c, "O") = "GR_DEF"
ElseIf Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96" And Cells(c, "S") > 150 Then
Cells(c, "O") = "GR_DEFHW"
ElseIf Cells(c, "C") = "51" Or Cells(c, "C") = "54" And Cells(c, "S") <= 150 Then
Cells(c, "O") = "GR_CA"
ElseIf Cells(c, "C") = "51" Or Cells(c, "C") = "54" And Cells(c, "S") > 150 Then
Cells(c, "O") = "GR_CAHW"
ElseIf Cells(c, "S") > 150 Then
Cells(c, "O") = "GRHW"
ElseIf Cells(c, "S") <= 150 Then
Cells(c, "O") = "GR"
End If
End If
``````
Rerate-v5.5-sample.xlsm
Excel & VBA ExpertCommented:
Hmm. Try this....
``````If Cells(c, "Y") = "GR" Then
If (Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96") And Cells(c, "T") <= 150 Then
Cells(c, "P") = "GR_DEF"
ElseIf (Cells(c, "C") = "9" Or Cells(c, "C") = "17" Or Cells(c, "C") = "22" Or Cells(c, "C") = "23" Or Cells(c, "C") = "25" Or _
Cells(c, "C") = "92" Or Cells(c, "C") = "96") And Cells(c, "T") > 150 Then
Cells(c, "P") = "GR_DEFHW"
ElseIf (Cells(c, "C") = "51" Or Cells(c, "C") = "54") And Cells(c, "T") <= 150 Then
Cells(c, "P") = "GR_CA"
ElseIf (Cells(c, "C") = "51" Or Cells(c, "C") = "54") And Cells(c, "T") > 150 Then
Cells(c, "P") = "GR_CAHW"
ElseIf Cells(c, "T") > 150 Then
Cells(c, "P") = "GRHW"
ElseIf Cells(c, "T") <= 150 Then
Cells(c, "P") = "GR"
End If
End If
``````
IT / Software Engineering ConsultantCommented:
Did you try the CASE statement version, feels a little easier to maintain and debug to me, but as long as you get a working solution I'm happy.

~bp
Commented:
did you try my formula?
Author Commented:
@aikimark - yes, I tried your formula, it didn't work for me.
Can you reopen?
Excel & VBA ExpertCommented:
Have you tried the modified code?
Author Commented:
@sktneer - I did and still didn't get any HW.
@bill - your code DID work!
Excel & VBA ExpertCommented:
Strange. But I got the correct output in col. T. :)
Commented:
it didn't work for me
What didn't work?
Are there more conditions that you haven't mentioned?
Do you have automatic calculation disabled?
Excel & VBA ExpertCommented:
@Euro5

You requested to reopen the question when my provided solution didn't work for you completely.
Then I provided you a modified solution that also didn't work for you as per your statement.

Then why did you select my solution as an accepted solution?
I really can't understand that and am very unhappy.

You should only pick a solution as an accepted solution once it works for you and resolves your issue and not just because someone has offered you a solution which doesn't work for you.

If the solution offered by Bill Prew worked perfectly for you, you should have picked only his solution as an accepted solution. This is injustice to him as he didn't get the right reward for his contribution.

Please don't do this in none of your future questions and be clear while selecting a solution.

Thanks
Excel & VBA ExpertCommented:
@aikimark

As you can see that he accepted the Bill's solution as an Accepted Solution and mine as an Assisted Solution.

I questioned him for accepting my solution as an assisted solution when it didn't work for him as per his earlier statement, though it worked when I tested it.

Regards.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.