1

I have written this code to automatically pop up a message box when the value of a cell is not equal to zero. This cell depends on the value of cell A minus the value of cell B.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("H60") <> 0 Then
MsgBox "Not Equal zero!!!!"
End If
End Sub

However, when the cell H60 is zero, the message box still continues to pop up. Why?

Zoe
  • 27,060
  • 21
  • 118
  • 148
koky
  • 31
  • 1
  • 2
  • 8
  • 2
    Change your `MsgBox` temporarily to `MsgBox "Not Equal zero!!!! Is " & Range("H60")`. I'm guessing that it isn't exactly zero. – YowE3K Sep 13 '17 at 03:52
  • If it's based on the difference of two cells, then why are you checking only one cell? Somehting like `If Range("A1").Value - Range("B1").Value = 0 Then ...` should be applied. – Michał Turczyn Sep 13 '17 at 05:18

1 Answers1

0

use .Value2

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("H60").Value2 <> 0 Then
    MsgBox "Not Equal zero!!!!"
End If
End Sub

I would however recommend you put some sort of test in there to only fire on certain cell changes, you don't want it firing every time anything is changed on the sheet.

You mentioned the cell relies on 2 other cells, you could have your test on those with something like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If target.address = "$I$60" or target.address = "$J$60" then
    If Range("H60").Value2 <> 0 Then
        MsgBox "Not Equal zero!!!!"
    End If
end if
End Sub

This will make it only fire if I60 or J60 are what was changed on the sheet, you can obviously change these to other cell references if you need, I assumed your formula is using I60 and J60

Dan Donoghue
  • 6,056
  • 2
  • 18
  • 36
  • 1
    I think `Worksheet_SelectionChange` is fired on every change in the sheet irrespective of what you write inside the `Sub`. The condition you put however runs a certain code inside the condition. Nevertheless the control will has to go till `End Sub` in all cases. – kaza Sep 13 '17 at 03:57
  • Yes the event is fired but the check and resulting msgbox will be omitted if it is not those cells that fired it. – Dan Donoghue Sep 13 '17 at 03:58
  • And TBH I would probably use the worksheet_change event, not the worksheet_selectionchange event – Dan Donoghue Sep 13 '17 at 03:59
  • 1
    Agree with you on function choice. On the other part I mis-took your answer, my bad. – kaza Sep 13 '17 at 04:01
  • 1
    The `Target` for `SelectionChange` will be the cell you are selecting, so using a `Target` of the cells that might be changed to ultimately change H60 won't work (because the change to H60 will occur after the selection). – YowE3K Sep 13 '17 at 04:01
  • All good, it is an interesting point you raise though, I mean we could use `If not blah blah then exit sub` which would probably be technically better but for the purpose of this exercise either works :) – Dan Donoghue Sep 13 '17 at 04:02
  • Good Point YowE3K, I have changed my answer to use `Worksheet_change` and I can't think of any scenario where the OP would want the other event, I believe it must have been used by the OP in error. – Dan Donoghue Sep 13 '17 at 04:04
  • 1
    There is one scenario - the two cells that are being subtracted could themselves be calculated by a formula. (But I don't think this resolves the issue - I can't think of a situation where `Value2` will be 0 when `Value` isn't, unless H60 is a date.) – YowE3K Sep 13 '17 at 04:06
  • In which case you would change your test but you would still use the worksheet_change event, not the Worksheet_SelectionChange event. – Dan Donoghue Sep 13 '17 at 04:07
  • 1
    :D A lot depends on what those other formulas are - imagine if one of those formulas was a `SUM` of several cells in an external workbook. Or it could be an `INDIRECT` referencing cells anywhere on the current worksheet. It starts getting hard to work out which cell needs to have the `Change` event on it. (But it is definitely better to use `Change` if the root cells that cause the calculation can be determined.) – YowE3K Sep 13 '17 at 04:10
  • 1
    I just found an elegant solution to use Worksheet_Change... https://stackoverflow.com/questions/4388279/how-can-i-run-a-vba-code-each-time-a-cell-get-is-value-changed-by-a-formula @YowE3K – kaza Sep 13 '17 at 04:13
  • 1
    Not foolproof for `INDIRECT` but at least you can determine if our earmarked range is in the immediate dependency tree of `Target` range passed in through `Worksheet_Change`. @YowE3K – kaza Sep 13 '17 at 04:18
  • That's a really nice solution :). Will add to my little bag of tricks for future use ;) – Dan Donoghue Sep 13 '17 at 04:20
  • @DanDonoghue Hi Dan, I tried your recommendations but it could not work for "Range("H60").Value2". This is because cell H60 formula is cell H58 minus cell F58. Cell H58 formula is summation of cell H7 to H57. Cell F58 formula is summation of cell F7 to F57. Any other recommendations? :) – koky Sep 13 '17 at 04:56
  • Hi all users, I have managed to find out the solution. After i round cell H58 and cell F58to 2 decimal places, my original solution works. Thank you for all the comments. :) – koky Sep 13 '17 at 06:18