If, And

  • Thread starter Thread starter M3Cobb
  • Start date Start date
M

M3Cobb

Can someone help me out with a fairly simple formula?
I want a message to pop up saying " Please fill in the price".

The function needs to say, If C10>0, and F10=0 then please fill in the
price.

It should be a simple function, but IF AND statements lose me!!
 
One way:

If Range("C10").Value > 10 And Range("F10").Value = 0 Then _
MsgBox "Please fill in the price"
 
If ((Range("C10").Value > 0) And (Range("F10").Value=0)) Then _
MsgBox "Please fill in the price"

The trickiest part of Ifs with Ands, Ors, or a combination of them is to
make sure the conditions get put in the right order to be executed properly.
For example:
If A=1 And B=2 Or C=3
Does this mean:
If (A=1 And (B=2 Or C=3))
Or does it mean:
If ((A=1 And B=2) Or C=3)
Or, worse, if there are Boolean variables or Variants involved, does it mean
If (((A=1) And B)=2) Or C) = 3

That is why I like using the brackets, even if they are redundant - it helps
me make sure the code is correct and helps me to see my intention if I go
back to debug the code later.
 
I would do it by putting the Cell values into variables and testing their
condition, something like this:

Range("C10").Select
A=ActiveCell
Range("F10").Select
B=ActiveCell

If A>0 And B=0 Then
MsgBox ("Enter Data In Cell F10")
End If

This would obviously need expanding with some validation of the datafor the
cells, but I think you should get the general idea.

HTH


Neil
www.nwarwick.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top