VBA comparing cell values dosen't work consistantly

michaelehoudek

New Member
Joined
Jan 15, 2016
Messages
21
I have the game scoring worksheet pictured below. Everything works fine up to 6 players. If I add another, the line "If Range("H" & rownum) <> Range("J11").Value Then" doesn't work as it should and triggers Exit Sub. I added the MsgBox line above to verify H and row matchs J11, which it always does. Can't firgure out why it works till J11 is 0.0007 or higher! Any ideas? Thanks, Mike

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rownum As Integer, CellValue As String, Game As String, MyInput As Integer, Selected
On Error GoTo ErrorHandler
'Sheets("Results").Protect UserInterfaceOnly:=True
Game = Sheets("Input").Range("Game")
rownum = ActiveCell.Row
Selected = ("$L" & "$" & (25 - MyInput))
numselect = Range("$A$1").Value
If Game = "Chickenfoot" Then
If (ActiveCell.Row) = numselect Then
Exit Sub
Else
        MsgBox (Range("H" & rownum).Value & "   " & Range("J11").Value)        'THIS LINE NORMALLY ISN'T HERE, JUST DONE TO CONFIRM VALUES ARE THE SAME
If Range("H" & rownum) <> Range("J11").Value Then
Exit Sub
End If
MyInput = InputBox("Domino number selection", "Enter Number Entering Score For, or Press Enter if on right row")
    ActiveSheet.Range("$A$1") = 25 - MyInput
If (ActiveCell.Address) = Selected Then
Application.EnableEvents = True
Exit Sub
 

Attachments

  • Game.png
    Game.png
    46 KB · Views: 6
Last edited:

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"
This looks like a floating point arithmetic rounding difference.

In the workbook, try the formula: =H13=J$11, and copy down. You should find there's a FALSE value somewhere, i.e. a tiny rounding difference?
 
Upvote 0
This looks like a floating point arithmetic rounding difference.

In the workbook, try the formula: =H13=J$11, and copy down. You should find there's a FALSE value somewhere, i.e. a tiny rounding difference?
Good idea. Came up as TRUE all the way down. This really has me perplexed! Thanks, Mike
 
Upvote 0
In that case, are you sure it's this piece of code that's the problem? You can put a breakpoint on the Exit Sub line to test.

VBA Code:
If Range("H" & rownum) <> Range("J11").Value Then
    Exit Sub
End If

You've only posted part of your code, and it's not at all clear what it's doing, or why you're using the SelectionChange event. You have Exit Sub in two other places. But what makes you sure that Exit Sub is being triggered at all?

The code looks a bit muddled in any event, e.g. this line occurs before MyInput has a value

Code:
Selected = ("$L" & "$" & (25 - MyInput))
Later on, you test:

Code:
If (ActiveCell.Address) = Selected Then
and my guess is that you meant to update Selected based on the value of the InputBox? But your code doesn't do this, so Selected still has its initial value.

I suggest you put a breakpoint at the top of your Sub and step through line by line to test what is happening.
 
Upvote 0
You're right about my muddled code. I'm no pro at this, I just keep trying things till I get it to work. Your comment about MyInput not having a value taught me a bit about how to walk through my code to see what doesn't make sense. Thank You for that!

Putting in the break point is how I found the line that broke down for me: If Range("H" & rownum) <> Range("J11").Value Then
The thing that I can't figure is why it doesn't work after 6 players, since H coulmn is calculated by multipling .0001 x $ op players and J11 does the exact same math. H coulmn only changes after I enter the first score.

Copying =H13=J$11 gives me true no matter how many players, yet when I added a VBA line MsgBox Range("H" & rownum).Value = Range("J11").Value it gives me false after 6 players, should be doing the exact same math I thought. I tried to subtract H13 from J11 and J11 from H13 in the VBA to determine what the difference was and the answer I got was -1.0842021724855E-19... Didn't help me determine what was going on.

However, your idea "try the formula: =H13=J$11, and copy down. You should find there's a FALSE value somewhere, i.e. a tiny rounding difference?" gave me an idea - I solved the problem by changing the line - If Range("H" & rownum) <> Range("J11").Value Then to - If Round(Range("H" & rownum).Value) <> Round(Range("J11").Value) Then NOW IT WORKS. I don't know why and it bugs me that I don't know why, but at least by trying to figure it out your line of thinking made it work and was valuable to me.

Thanks again, Mike
 
Upvote 0
Great, I'm glad you worked it out.

The thing that I can't figure is why it doesn't work after 6 players, since H coulmn is calculated by multipling .0001 x $ op players and J11 does the exact same math ...

... I tried to subtract H13 from J11 and J11 from H13 in the VBA to determine what the difference was and the answer I got was -1.0842021724855E-19...

Here's the explanation: Floating-point arithmetic may give inaccurate result in Excel - Microsoft 365 Apps

And an example to illustrate:

Cell Formulas
RangeFormula
B1B1=1/3
C1:C26C1=A1/3
D1:D26D1=C1=B1
E1:E26E1=IF(D1,"",B1-C1)
B2:B26B2=B1+1/3
 
Upvote 0
Solution
Thank you! It was really bugging me that while I'm not a math expert, I knew the two numbers should be exactly the same. I appreciate your tenacity!
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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