VBA - If Cell value is changed

hmltnangel

Active Member
Joined
Aug 25, 2010
Messages
290
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Kind of a follow on from a query I had a few weeks back. But an additional piece is needed to the code now, and I keep getting it the wrong way around. So hopefully someone can offer the right method for me.

This is the code.

VBA Code:
If Target.Value > 0 Then
            If Target.DisplayFormat.Interior.Color = 8696052 Then
            MsgBox "Blah Blah Blah"
            End If
End If

The initial part of the code, if the cell colour was orange works great. It pops up the message box as required. However the change thats needed now is to only have the pop up box if the value is changed, and the colour is orange. I did try adding a little bit to the front of the code, but its not working if the value is changed, it only works if the value is greater than 0 and Orange.

thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Value > 0 Then
        If Target.Interior.Color = 8696052 Then
            MsgBox "Blah Blah Blah"
        End If
    End If
End Sub
 
Upvote 0
Sorry mumps, I didnt include the full code. The DisplayFormat piece is to allow for Conditional Format of the cell.

Basically, if the cell value gets changed, and the colour is orange, then Msg Box.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
If Target.Value > 0 Then
            If Target.DisplayFormat.Interior.Color = 8696052 Then
            MsgBox "Blah Blah Blah"
            End If
End If

End Sub
 
Upvote 0
This works for me:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Value > 0 Then
        If Target.DisplayFormat.Interior.Color = 8696052 Then
            MsgBox "Blah Blah Blah"
        End If
    End If
End Sub
 
Upvote 0
Solution
For me, it works as follows:

If you change the value, and its orange, I get no msg box.

If you change the value, and its orange, then reselect the cell you get a msg box.

Can it work if you change the value, and its orange, then you get the msg box? (without having to click off the cell, and reselect it?)
 
Upvote 0
For me, it works as follows:

If you change the value, and its orange, I get no msg box.

If you change the value, and its orange, then reselect the cell you get a msg box.

Can it work if you change the value, and its orange, then you get the msg box? (without having to click off the cell, and reselect it?)
That behavior you are describing is when you use the "Worksheet_SelectionChange" event.
Look at mumps last code. He is using "Worksheet_Change", not "Worksheet_SelectionChange".
Did you make that change to your code?
 
Upvote 0
With Worksheet_Change the target is the cell you're coming out of
With Worksheet_SelectionChange the target is the cell you're going into
 
Upvote 0
Quite simply, the "Worksheet_Change" event procedure fires whenever a cell's value is changed (either manually or by code, not by a formula or link).
So the "Target" cell in this case is the cell being changed.

The "Worksheet_SelectionChange" event procedure fires whenever a cell is selected.
The "Target" cell in this case is the cell being selected.
 
Upvote 0
🫣 Sorry I missed that Mumps. Joe, thanks for the pointing it out. Such a subtle change I completely missed it.

You two are brilliant. Thanks

👍
 
Upvote 0
🫣 Sorry I missed that Mumps. Joe, thanks for the pointing it out. Such a subtle change I completely missed it.

You two are brilliant. Thanks

👍
You are welcome.
Glad we were able to help!

Yes, a subtle change, but one that changes how it works entirely!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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