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

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.

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!

how do I change the position of my image with coordinate ranges in vba

I'm trying to program a Chess-Game in VBA. I'd like to change the position of the figures with mousemove. I can already move them but It would be great if I could release the figure and the figure so jumped into the middle of the field.
I have no Idea how do solve the Problem
Thanks in advance
PS: Sorry for my bad english
Best case scenario - use the macro recorder. Copy the figure and paste it somewhere.
See the code. Then make sure that it works, only in the given range of the chessboard. Then make an event like this:
Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A1:H8")) Is Nothing Then Exit Sub
Debug.Print Target.Address
End Sub
And try to build the recorded code in Target.Address. The event is for the right click of the mouse. It should be put in the Worksheet part of the VBEditor.

Excel VBA DoubleClick

I'm working on a work project where I have an excel sheet with values that turn red when they are out of spec. What I'd like to do is be able to double click on a cell and have the sheet in my workbook pop up that has trending data on it. I have already created the sheet with the graph on it. Long story short, I'd like to be able to double click on a specific cell and have it bring up the corresponding sheet.
I have tried this code, and it will not work. Is anyone able to maybe write code from scratch or alter the code so I could use it? The cell I'm trying to click on is N9, and the sheet I want it to open is called "Alpha Final Rinse"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
Sheets("Alpha Final Rinse").Select
End Sub
I'm doing this in Excel 2013. Thank you!
If you only want N9 to be able to switch focus to another worksheet, isolate Target with the Intersect method.
In the data worksheet's code sheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("N9")) Is Nothing Then
cancel = True
Worksheets("Alpha Final Rinse").Activate
End If
End Sub
Note that cancel = True is necessary to stop the user entering in-cell edit mode (assuming that has been enabled in Options).
Your code will work if:
it is installed in the worksheet code area of your data sheet (the sheet whose cells you are double-clicking)
macros are enabled
the file type is .xlsm rather than .xlsx

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 to run automatically Excel macros?

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub
Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.