ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Macro that runs when a cell is selected (https://www.excelbanter.com/excel-discussion-misc-queries/205237-macro-runs-when-cell-selected.html)

Daniel Bonallack

Macro that runs when a cell is selected
 
I have a macro that I want to run when the user selects a cell. Can someone
give me the code that would do this?

Thanks in advance!

Daniel


Jim Thomlinson

Macro that runs when a cell is selected
 
This runs when A1 is selected. Right click the sheet tab and select view
code. Paste the following...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
MsgBox "Tada"
End If
End Sub
--
HTH...

Jim Thomlinson


"Daniel Bonallack" wrote:

I have a macro that I want to run when the user selects a cell. Can someone
give me the code that would do this?

Thanks in advance!

Daniel


Dave Peterson

Macro that runs when a cell is selected
 
You can use a worksheet event:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Me.Range("A1:B3")) Is Nothing Then
Exit Sub
End If

Call MacroNameHere

End Sub

This checks to see if you selected any cell(s) in A1:B3. If you did, then it
calls MacroNameHere.

Rightclick on the worksheet tab that should have this behavior. Select view
code. Paste this code into the code window.

Change the range to check to what you want and change the name of the macro,
too.

Daniel Bonallack wrote:

I have a macro that I want to run when the user selects a cell. Can someone
give me the code that would do this?

Thanks in advance!

Daniel


--

Dave Peterson

Daniel Bonallack

Macro that runs when a cell is selected
 
Thank you Jim and Dave!
regards
Daniel

"Dave Peterson" wrote:

You can use a worksheet event:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Me.Range("A1:B3")) Is Nothing Then
Exit Sub
End If

Call MacroNameHere

End Sub

This checks to see if you selected any cell(s) in A1:B3. If you did, then it
calls MacroNameHere.

Rightclick on the worksheet tab that should have this behavior. Select view
code. Paste this code into the code window.

Change the range to check to what you want and change the name of the macro,
too.

Daniel Bonallack wrote:

I have a macro that I want to run when the user selects a cell. Can someone
give me the code that would do this?

Thanks in advance!

Daniel


--

Dave Peterson



All times are GMT +1. The time now is 04:19 AM.

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