Link to home
Start Free TrialLog in
Avatar of Frank Freese
Frank FreeseFlag for United States of America

asked on

If (or case?) help

Folks,
I have a command button that will check the values in a specific range to make sure they are correct. The cell are formatted number with 2 decimal places.
The range is from D7:D14 and cell D15.
If the value is any cell does not match the answer, I'd like to tell the user that the answer is incorrect and to check their formula or function. For example, the answer for D7 maybe 13.23 but the user got 565.
The only cells not protected will be those that have formulas or functions. I'm not sure a Case or If statement is best to use and what the code would lokk like.
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

Do you have a sample layout?
Avatar of Frank Freese

ASKER

Before we go there let me run this by you.
I created a module called CheckFormulaFunction as follows:
Sub CheckFormulaFunction()
MsgBox ("Your formula or function did not generate the correct answer."), vbOKOnly, "Incorrect"
End Sub

Open in new window


Then I created a command button to check the answer. Here's that code:
Private Sub cmdCkAnserAverageCon_Click()
If D7 <> 13.23 Then
    CheckFormulaFunction
    End If
    Else
    If D8 <> 15.35 Then
    CheckFormulaFunction
    End If
 End Sub

Open in new window


This "appears" to work, but is it right or is there a better way?
My mistake. This is not working past the first IF

That's not working either. You see D7 is a formula. So how do I resolve this
For starters, you need to remove the ELSE.

Next, how are you telling them which cell is wrong?  With this method, you will just get 2 popup warnings but they don't tell you which ones are wrong.
Now, if you don't mind having to populate these before hand, you can go with:

Private Sub cmdCkAnserAverageCon_Click()
    If D7 <> 13.23 Then
        MsgBox ("Your formula or function in cell D7 did not generate the correct answer."), vbOKOnly, "Incorrect"
    End If

    If D8 <> 15.35 Then
        MsgBox ("Your formula or function in cell D8 did not generate the correct answer."), vbOKOnly, "Incorrect"
    End If
End Sub

Open in new window

OK - but the value returned from the formula is <>13,23 in reality. It might be 13.2345657 but the cell format is set to 2 decimal places. So you only see 13.23.
I'm consider defining a variable type to a number restricted to 2 decimal places but I can't find out how to do that. From there I can take the value returned to the 2 decimal places and comapre that with a coded 13.23. Can you help me there?
ASKER CERTIFIED SOLUTION
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks - I caught myself trying to use the range at one time but failed. I can see why now.
Appreciate it