MsgBox when value change

van0912

New Member
Joined
Jul 27, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I would like to trigger a msgbox when a cell in a worksheet range change from "OK" or "N/A" to "NOK". I have the following code but it is not working (message error is "Object required")... Could you please help me?

Sub Msgbox_statutchange()

Dim R As Range

Set R = Sheets("List").Range("R4:R500")

If Intersect(Target, R) Is Nothing Then Exit Sub

If Target.Value = "NOK" Then

MsgBox "Statut has changed to " & Target.Address


End If

End Sub

Thank you!!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

your code is failing as you do not seem to be defining "Target" anywhere ?

where should you get Target from, and what should it be ? Should you be passing it into this subroutine as a parameter ?

Hope that helps
Rob
 
Upvote 0
Hi,

your code is failing as you do not seem to be defining "Target" anywhere ?

where should you get Target from, and what should it be ? Should you be passing it into this subroutine as a parameter ?

Hope that helps
Rob
Hello Rob,

Thanks for replying. I'm not sure to understand as I'm just starting on VBA.
I would like to have a msgbox when value in Range R (rows 4 to 500) changes from "OK" or "N/A" to "NOK" (range R corresponds to "status").

Thank you for your help,
Vanessa
 
Upvote 0
The body of your code looks good, but it needs to be contained within a "Worksheet_Change" event procedure, which is VBA code that runs automatically when some cell is changed on the sheet (the code MUST also be put in the "Sheet" module of the sheet you would like it to run against, and not in a General Module.

So the code should look like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim R As Range

Set R = Sheets("List").Range("R4:R500")

If Intersect(Target, R) Is Nothing Then Exit Sub

If Target.Value = "NOK" Then
    MsgBox "Status has changed to " & Target.Address
End If

End Sub
If it does not run automatically, then you have probably not put the code in the correct sheet module (the "List" sheet module).
 
Upvote 0
Solution
The body of your code looks good, but it needs to be contained within a "Worksheet_Change" event procedure, which is VBA code that runs automatically when some cell is changed on the sheet (the code MUST also be put in the "Sheet" module of the sheet you would like it to run against, and not in a General Module.

So the code should look like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim R As Range

Set R = Sheets("List").Range("R4:R500")

If Intersect(Target, R) Is Nothing Then Exit Sub

If Target.Value = "NOK" Then
    MsgBox "Status has changed to " & Target.Address
End If

End Sub
If it does not run automatically, then you have probably not put the code in the correct sheet module (the "List" sheet module).
Thank you Joe, it works in a "Sheet" module :)
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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