compare values

  • Thread starter Thread starter RobcPettit
  • Start date Start date
R

RobcPettit

Hi, is it possible with vba, to compare values, give or take 1. eg x =
15 and y = 16. If x(+ or -1) = y(+ or -1) then............ In reality
what Ihave is something like 16.1111111 and 16.111112, I just want the
whole numbers, eg the 16, the .1111 is not needed, although its part of
the eqaution.
Regards Robert
 
Hi Robert

This is not a VBA solution but may satisfy your needs...

Insert a new column (if needed) & type in:
=IF(ROUND(A1,0)=ROUND(B1,0),"approx match", "rounded values don't
match")

Change the cell references as required & the True/False statements.

This will compare the two values after rounding them to the nearest
whole number. Depending on your exact requirements, either of the
functions "roundup(reference,# of decimals)" or "rounddown(reference,#
of decimals)" / "int(reference)" may be more appropriate to use in your
If statement (type "round" into Excel help for more details).

Rob Brockett
NZ
Always learning & the best way to learn is to experience...
 
What you want is one of the following:
1) To round up or down to the nearest whole number
If Round(x,0) = Round(y,0) Then....
e.g. Round(16.111,0)=16 while Round(16.500001,0)=17
2) To truncate any decimal points:
If Int(x) = Int(y) Then...
e.g. Int(16.99999) = 16
3) To accurately check if y is in a +/- 1 unit range around x:
If ABS(x-y)<=1 Then...
 
Thankyou both for your replys, Ive gone with number 2, f Int(x) =
Int(y) Then.. This is perfect for me.
Regards Robert
 
Pleased to be able to help :-)

Rob Brockett
NZ
Always learning & the best way to learn is to experience...
 

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