Run macro on cell being changed by user - vba

I am trying to make a macro automatically run if any cells in a range is changed (C5 to c25).
As you see in the code below, it should automatically bring up a message box asking the user whether or not to continue (if the user says yes then runs the macro).
I cannot get the code to start running though once I have changed any one of the cells (from c5 to c25).
Here is the code - it is not all of my own:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("C5:C25")) Is Nothing Then Reminder
End Sub
Sub Reminder()
'
' Reminder Macro
'
response = MsgBox("Do you want to set a reminder in Outlook for when the next update is required? If yes, make sure your Microsoft Outlook is open.", vbYesNo)
If response = vbNo Then
MsgBox ("You selected 'No'")
Exit Sub
End If
'Rest of my macro code goes here...
End sub
Thank you!

Ensure your code is in the Worksheet's code module. From the comments above, you indicate it's in Module 3. It needs to be moved to the worksheet's code module.

Try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C5:C25"), Target) Is Nothing Then
Call reminder
End If
End Sub

Your code works fine, make sure you put it in the Worksheet object. Also I believe the code won't be active until you save as an xlsm, close, and reopen.

Related

Automatic execution of Macro based on cell change

I'm trying to run a macro every, time the selection of a dropdown menu in a cell changes. I've tried to follow this solution: automatically execute an Excel macro on a cell change
by entering the following code:
Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then Call Testsub
End Sub
Public Sub Testsub()
MsgBox "Testmessage", , "Testbox"
End Sub
Cell A1 contains the Dropdown menu that should trigger the macro "Testsub". However, nothing happens when I change the selection. I'd be very grateful for any idea, what might cause the problem.
The comment of user11138753 nailed it. I edited the code at the wrong place.
Thank you very much!

Run macro when cell changes pass value

I have this certain code on a worksheet event cell change. It finds certain data that I have on a Worksheet when I type an id_parameter on cell A1.
After data is found, it writes it on this worksheet.
Now I'd like to run a different macro when I change the values on column C of the data.
I'm not able to find the proper procedure. Do_something() receives the value changed in column C. I hope I explained it clearly enough.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Call Search_data
End If
'Data is written in B2:E10
If Not Intersect(Target, Range("C2:C10")) Is Nothing Then
Call Do_something(Target.Value)
End If
End Sub
The code you have posted works as intended. The issue is most probably due to Search_Data and or Do_Something.
You change the cell A1 which then changes all cells from B2:E10. Including the changed C2:C10 so make sure that the change_event cannot be triggered again by this.
Option Explicit
Public EventsDisabled As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not EventsDisabled Then
EventsDisabled = True
If Not Intersect(Target, Range("A1")) Is Nothing Then
Call search_data
End If
'Data is written in B2:E10
If Not Intersect(Target, Range("C2:C10")) Is Nothing Then
Call do_something(Target.Value)
End If
End If
End Sub
The Public variable EventsDisabled will make sure that the change event runs when a user input is given but not if a Worksheet_Change event changes the worksheet. Otherwise you will run into all sorts of weird loopings which might be infinite or at least take a long time. Your issue will most probably also be related to this. Make sure that your Do_Something sub has the right input type. You should set a few breakpoints and open the local window under:
view>local window here you can see all variables and objects and their type. You can also type:
debug.print typename(Target.value)
into the direct window and then see what type you need to type into the Do_Something signature.

Worksheet_Change doesn't fire up when streaming live data

I have read many posts around this topic, however nothing seems to work for my scenario
I would like to Call Sub upon cell change (B2) which contains live data feed from external source -last updated:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B2")) Is Nothing Then
Call SubName
End If
End Sub
I went through numerous posts suggesting to check if Äpplication.EnableEvents = True, or create function to detect the change ( which does work, however I cannot call sub within function) - with no success.
Interestnigly enough, when I click on B2 and press enter - it executes the sub
Thanks
Say cell A1 is updated by streaming. The update will not trigger either:
Worksheet_SelectionChange
Worksheet_Change
What you need to do is setup an equation somewhere:
=A1
When A1 is refreshed, the formula cell will re-calculate and you can detect it with the Calculate Event.
you can simply go
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then SubName
End Sub

Running Excel Macro On Hyperlink Click

I'm trying to assign macros to hyperlinks I have set up in excel. I have the hyperlinks linking back to the same cell that contains the hyperlink. Based on code examples I've found I've come up with this code block:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Address
Case "$B$3"
MsgBox ("Test")
Case "$Z$3"
MsgBox ("Test")
Case Else
Exit Sub
End Select
End Sub
Nothing is happening when I click either hyperlink. I have tried running Application.EnableEvents = True as well and still can't get anything to fire. Also, I have verified the code block is set in the correct worksheet module. Any help would be appreciated. Thanks!
EDIT: Here is a screencap of the hyperlink dialog as an example for the hyperlinks I have set up:
I have tried both "Z3" and $Z$3 for the address (both ways direct me to the cell properly, it just doesn't fire the macro.)
Your code works if the code is in the worksheet code area
Right-click the tab at the bottom and:

How can I run a Macro when the value in a specific cell changes?

Let me preface by saying that I am very new to VB...
I am trying to run a macro whenever the value in a certain cell changes. I've read up on how to do this, but can't seem to get it to work. I have entered the following code into the private module of the worksheet object:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
Application.Run _
"'Amex Payments_Experiment.xlsm'!SelectCells"
End If
End Sub
C5 is the cell I am trying to monitor for change.
"SelectCells" is the macro I want to run.
"Amex Payments_Experiment.xlsm" is the name of the file.
When I change the value in C5 nothing happens. Some help would be great. Thanks!
UPDATE:
Cyberkiwi - No, that is not exactly how I did it, but when I follow you're instructions I do find the code where you say it should be. To get to the private module of the worksheet object I right clicked the sheet tab at the bottom, selected "view code", then selected "worksheet" from the dropdown in the top center of the page.
User587834 - Yes. Macro's are enabled.
Any other suggestions?
if you use Excel2007 be sure that macros are enabled, by default Excel 2007 deactivate macro execution for new workbook.
for that try to execute any other macro to be sure that macros are enabled.
I have entered the following code into the private module of the worksheet object:
How exactly have you done that? As below?
Alt-F11 to switch to code view
On the left, double-click on the target sheet
On the right, do you see the code you entered? if not, proceed to next step
Paste the code block
This code works Ok for me
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
MsgBox "hello"
End If
End Sub
Maybe the problem is with your Application.Run:
- select the line containing the intersect and click F9 to switch on Debug, then try changing a cell to see if you get to the sub.
If you never reach that line then you have not got the code in the worksheet clkass module, or you have switched off events, or macros are disabled or ...
Check the Application.EnableEvents property, and set it to True if required.