Why Does Excel Say Cell Values Are Not Equal When They Are?

nira123

New Member
Joined
Feb 21, 2019
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Not sure why it says cell values are not equal when they are equal. Following is my code and cell values are attached. I need to count cells values first and make sure first coordinate and last coordinate equal . Many thanks.


Sub coggv()

Dim i, j, tn As Integer
Dim a1, aa, ixv, iyv, cxx, cyy, cxv, cyv, cx1, cy1 As Double

n = 0
k = 0
aa = 0
ixv = 0
iyv = 0
cxx = 0
cyy = 0
u = 0


Do While Cells(6 + n, 12).Value <> ""

n = n + 1
Loop
tn = n
If (Cells(6 + tn - 1, 12) <> Cells(6, 12) Or Cells(6 + tn - 1, 13) <> Cells(6, 13)) Then

MsgBox ("Xn, Yn must be equal to X0, Yo")

Else
bla bla......

endif
 

Attachments

  • Capture2.JPG
    Capture2.JPG
    20 KB · Views: 20

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
First of all, this is wrong:

VBA Code:
Dim i, j, tn As Integer
Dim a1, aa, ixv, iyv, cxx, cyy, cxv, cyv, cx1, cy1 As Double

This declares only tn as an integer and only cy1 as a double. The others are all variants.

Also, it's better to ignore Integers and use Longs (and the same rationale goes for Singles and Doubles).

You need to do it this way:

Code:
Dim i Long, j Long, tn As Long
Dim a1 As Double, aa As Double, ixv As Double, iyv As Double, cxx As Double, cyy As Double, cxv As Double, cyv As Double, cx1 As Double, cy1 As Double

Also, further down, use Cells.Value:

Code:
If (Cells(6 + tn - 1, 12).Value <> Cells(6, 12).Value Or Cells(6 + tn - 1, 13).Value <> Cells(6, 13).Value) Then

Second, you're probably running into the famous rounding error in the 15th significant digit. You should round these values or take other measures to eliminate tiny meaningless differences. Something like:

Code:
If Abs(Cells(6 + tn - 1, 12).Value / Cells(6, 12).Value) < 0.000001 Then
 
Upvote 0
Hi Jon,
Many thanks for your response and correcting my mistakes. Wow, 15th significant digit did not know about that. I will try your suggestions and check whether it would help.

Kind Regards
 
Upvote 0
Hi Jon, with your last point( 15th significant digit ) it worked brilliantly. I imported points from AutoCad, in excel it seemed equal even after clicking on the particular cells. I understand now it was due to rounding error in the 15th significant digit. Thank you so much.

Kind Regards,
Shan
 
Upvote 0
First of all, this is wrong:

VBA Code:
Dim i, j, tn As Integer
Dim a1, aa, ixv, iyv, cxx, cyy, cxv, cyv, cx1, cy1 As Double

This declares only tn as an integer and only cy1 as a double. The others are all variants.

Also, it's better to ignore Integers and use Longs (and the same rationale goes for Singles and Doubles).

You need to do it this way:

Code:
Dim i Long, j Long, tn As Long
Dim a1 As Double, aa As Double, ixv As Double, iyv As Double, cxx As Double, cyy As Double, cxv As Double, cyv As Double, cx1 As Double, cy1 As Double

Also, further down, use Cells.Value:

Code:
If (Cells(6 + tn - 1, 12).Value <> Cells(6, 12).Value Or Cells(6 + tn - 1, 13).Value <> Cells(6, 13).Value) Then

Second, you're probably running into the famous rounding error in the 15th significant digit. You should round these values or take other measures to eliminate tiny meaningless differences. Something like:

Code:
If Abs(Cells(6 + tn - 1, 12).Value / Cells(6, 12).Value) < 0.000001 Then


Following is the one I used to make that work. Hope this would be helpful for someone who got the same issue. It did not work well even I entered equal values manually in my worksheet until I tried Jon's last suggestion. Full credit to Jon.

VBA Code:
v1 = Abs(Cells(6 + tn - 1, 12).Value / Cells(6, 12).Value)
v2 = Abs(Cells(6 + tn - 1, 13).Value / Cells(6, 13).Value)

If ((v1 < 0.99999 Or v1 > 1.00001) Or (v2 < 0.99999 Or v2 > 1.000001)) Then


MsgBox (" Your Polygon is not closed: Xn, Yn must be equal to X0, Yo  ")

Else
'bla bla....

end if
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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