Automatic execution of Macro based on cell change - vba

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!

Related

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.

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:

Run macro on cell being changed by user

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.

Circle Invalid-data Macro

I have a worksheet with many dependant dropdowns that are liable to having invalid data if the initial drop down is altered. I would like a simple auto-macro that would automatically run the "circle invalid data" command every time a cell change within a specified range (D8:T800) is detected.
It sounds fairly straightforward but I am not sure how to do this.
Question - as this macro will run every single time a cell is amended would this macro slow down the worksheet?
EDIT:
Also: as this might be slow,is there a way we can run this command over a selected range?
Your thoughts thanks.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("D8:T800")) Is Nothing Then
Me.CircleInvalid
End If
End Sub
Note that CircleInvalid applies to the whole sheet, so while this code only triggers when a cell inside D8:T800 changes, all invalid cells on the sheet will be circled
Try placing this code inside your Worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
' Check target range has changed
If (Not Intersect(Target, Range("D8:T800")) Is Nothing) Then
' Prevent recusrive looping
Application.EnableEvents = False
' Refresh validation circles
Target.Worksheet.CircleInvalid
Application.EnableEvents = True
End If
End Sub
Note that this will NOT work if the values in those cells change due to a calculation from outside the specified range.
Also, the CircleInvalid method applies to the whole worksheet.
You could try editing the code in the conditional to 'do something' if the Target is validated — this would result in you changing the format of invalid cells instead of having the red circles around them.
**PSEUDO-CODE**
For each cell in Target.Range
cell.colour = bright red
Next cell

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.