VBA AND Logical Operators

Mike54

Active Member
Joined
Apr 17, 2002
Messages
258
Could someone please give me a bit of guidence, I don't understand why the second of these two dosen't work, I'm trying to meet set criteria in b5 using AND the first works if both conditions are met in two cells but bur won't on one cell?

Private Sub PTIColours_Click()
If Range("b5") > 0 And Range("c5") < 0.149 Then
Range("a5").Interior.ColorIndex = 54
End If
End Sub

But this dosen't

Private Sub PTIColours_Click()
If Range("b5") > 0 And Range("b5") < 0.149 Then
Range("a5").Interior.ColorIndex = 54
End If
End Sub


I have tried searching for a good example of how to use logical operators on Mr Excel however the search is frustrated by "Small Words" in the search field ie IF, AND, NOT ,OR are too short to search.
If anyone knows a good example, could the please direct me.

Many thanks
Mike
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
For the AND to return true, both conditions have to be satisfied.

If you only need 1, try OR
 
Upvote 0
works for me! something else must be going on...
 
Upvote 0
But I want both conditions to be true ie That Range B5 is both greater than 0 AND less than 14.9

Mike
 
Upvote 0
I wonder if it would work better if you put the value in a variable and ran the comparison on that?

Mind you it works as is here too.
 
Upvote 0
Okay, thanks ExcelR8R I'll give that a try.

Does anyone know where I can get an good simple example of how IF, AND, NOT & OR all work & in relation to each other. We did this in college and I can't find the examples.
 
Upvote 0
Mike you currently have less than 0.149 rather than as stated in your second post "less than 14.9" - is this possibly the source of your error? Otherwise, please could you define "doesn't work" - do you get an error or just nothing happens? What value do you have in the cell B5 when you run this? The syntax is correct for your information.
 
Upvote 0
Hi Richard and thanks, I'm working in percentages therefore my 0.149 is 14.9% but you're right I was testing code with the wrong input in the test cell, Doh!

However I have 160 rows & 4 Columns of data (each is a %) so 640 cells and I need to break them down in to seven groups 0-14.9% 14.9-28.6% Etc. to 100 and the Colour Format the data entry .

So Thanks for the help, I think I may be back

Mike
 
Upvote 0
Mike

Take a look at Select Case - you should be able to use this:

Code:
Select Case Range("B5").Value
  Case Is < 0
    'code here for less than zero - leave blank if you don't want anything to happen
  Case Is < 0.149
    Range("A5").Interior.ColorIndex = 3
  Case Is < 0.286
    Range("A5").Interior.ColorIndex = 5
  Case Is < 0.450
    Range("A5").Interior.ColorIndex = 7
  '...
  Case Else
    'other code for none of the above
End Select

Make sense?
 
Upvote 0
Wow that looks interesting, thanks I have play with that, looks very useful

Regards

Mike
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top