I want to have a macro fire upon a certain event: Range("C3") is changed. How would I program this?
I want to have a macro fire upon a certain event: Range("C3") is changed. How would I program this?
If Target.address=$C3$ then
'''do something
Need to put AB33's code inside ThisWorkbook and apply it to the Worksheet_Change event
How do I apply it to worksheet change event?
First you have to put the code listed below in the right place. Make sure you click on the correct VBA project and then click on the 'This Workbook' option. See the file below.
This Workbook..JPG
bstier![]()
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Not Intersect(Target, Range("C3")) Then MsgBox ("Cell C3 has been changed") 'You can change this line to the code that you want to be executed when C3 is changed. Else End If End Sub
Put some code in the Worksheet change event like bstier suggested to catch the change, but since you know the exact cell you can use:
Also, bstier I think Intersect will return Nothing if there are no intersecting cells so I think you need to check that it's not Nothing, otherwise it will fail:![]()
If Target.Address = Range("C3").Address Then 'Cell changed Else 'Cell not changed End If
![]()
If Not Intersect(Target, Range("C3")) Is Nothing Then 'Cell changed Else 'Cell not changed End If
Last edited by AKK9; 02-14-2013 at 05:20 PM.
.
- AKK9 -
Sorry, The code worked just fine when I changed C3 but when I changed any other cell I would get an error. So here is the corrected code to ignore the change to other cells.
![]()
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) On Error GoTo DoNothing If Not Intersect(Target, Range("C3")) Then MsgBox ("Cell C3 has been changed") 'Change this section of the code if you want additional code to be processed. Else Exit Sub End If DoNothing: End Sub
Why not use this in the worksheet module?
When you use the workbook module, you also have to define the sheet it has to apply to.![]()
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address(0, 0) = "C3" Then MsgBox ("Cell C3 has been changed") End Sub
For example
![]()
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Sh.Name = "Sheet1" Then If Target.Address(0, 0) = "C3" Then MsgBox ("Cell C3 on " & Sh.Name & " has been changed") End If End Sub
Last edited by Marcol; 02-14-2013 at 06:32 PM.
If you need any more information, please feel free to ask.
However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....
Also
اس کی مدد کرتا ہے اگر
شکریہ کہنے کے لئے سٹار کلک کریں
If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.
I got in a hurry before and didn't check to see if the code worked properly when I changed a cell other than C3. When I did this after the post I realized that the code was crashing. Since we don't care about the change of the other cells I used an On Error Goto statement to fix this. AKK9 caught this as well as I did but I have had trouble all afternoon with the Excel Forum site not showing all posts and allowing me to make timely corrections. Thanks for the double check AKK9
Anyway, this code should be in the Sheet1(Sheet1) and not the ThisWorkbook. Thanks Marcol
![]()
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) On Error GoTo DoNothing If Not Intersect(Target, Range("C3")) Then MsgBox ("Cell C3 has been changed") 'Change this section of the code if you want additional code to be processed. Else Exit Sub End If DoNothing: End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks