ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Attaching Macro to Cell instead of button? (https://www.excelbanter.com/excel-discussion-misc-queries/156338-attaching-macro-cell-instead-button.html)

Satchmo2006

Attaching Macro to Cell instead of button?
 
Hello,
I have a cell that is a list. Instead of attaching a Macro to a button, I
would like it to run everytime a new selection from this list is selected. Is
this possible? If so, how?
Much thanks!

JLatham

Attaching Macro to Cell instead of button?
 
Yes, use the worksheet's _Change() event handler. To get there, choose the
sheet, right-click the name tab and choose [View Code] from the list. Copy
the code below and modify as needed (specifically, change the address to the
cell with your list, and either put the code to execute within the If...End
If statements, or if code already exists in a public module, then just call
that Sub from within the If ... End If statements.

Note that this will run even if someone chooses the same item in the list
over and over again, not just another choice from the list.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$2" Then
'code to execute here
MsgBox "D2 changed"
End If
End Sub


"Satchmo2006" wrote:

Hello,
I have a cell that is a list. Instead of attaching a Macro to a button, I
would like it to run everytime a new selection from this list is selected. Is
this possible? If so, how?
Much thanks!


[email protected]

Attaching Macro to Cell instead of button?
 
On 29 aug, 22:36, Satchmo2006
wrote:
Hello,
I have a cell that is a list. Instead of attaching a Macro to a button, I
would like it to run everytime a new selection from this list is selected. Is
this possible? If so, how?
Much thanks!


You mean something like this ??
example macro xx reacts to changes on sheet 1

Sub xx()
Range("c1").Value = Range("a1").Value * Range("b1").Value
End Sub

groeten. Hans


(click on tab view code . in top line the following)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xx
End Sub



All times are GMT +1. The time now is 04:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com