ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro run if and cell in range is selected in VBA (https://www.excelbanter.com/excel-programming/310407-macro-run-if-cell-range-selected-vba.html)

Celtic_Avenger[_37_]

Macro run if and cell in range is selected in VBA
 

Hi.

Can someone please help.

I have used the made a simple macro to show a msgbox if any cell on th
page is selected, by placing it in the Selection_Change part of th
Sheet Code.

On another sheet I want 3 different macros to run if any cell in
different ranges are selected.

Is there a way to do this?

Thanks

Celtic_Avenger
:confused: :confused: :confused: :confused: :confused

--
Celtic_Avenge
-----------------------------------------------------------------------
Celtic_Avenger's Profile: http://www.excelforum.com/member.php...fo&userid=1410
View this thread: http://www.excelforum.com/showthread.php?threadid=26125


Alex J

Macro run if and cell in range is selected in VBA
 
Celtic,
Try this. It is not the most sophisticated for implementing the three
macros, but the key here is to use the INTERSECT function to find out if the
Target range (the selection) is within one of your three ranges on the
sheet.

AlexJ

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim ShtRng(1 To 3) As Range
Dim Isect As Range
Dim i As Long

Set ShtRng(1) = Me.Range("A1:B2")
Set ShtRng(2) = Me.Range("D1:E2")
Set ShtRng(3) = Me.Range("G1:H2")

For i = 1 To 3
Set Isect = Intersect(Target, ShtRng(i))
If Not Isect Is Nothing Then
Call DoSomething(i)
Exit For
End If
Next i
Erase ShtRng


End Sub

Sub DoSomething(Idx As Long)
Select Case Idx
Case Is = 1
MsgBox "Macro 1"
Case Is = 2
MsgBox "Macro 2"
Case Is = 3
MsgBox "Macro 3"
End Select
End Sub


"Celtic_Avenger" wrote in
message ...

Hi.

Can someone please help.

I have used the made a simple macro to show a msgbox if any cell on the
page is selected, by placing it in the Selection_Change part of the
Sheet Code.

On another sheet I want 3 different macros to run if any cell in 3
different ranges are selected.

Is there a way to do this?

Thanks

Celtic_Avenger
:confused: :confused: :confused: :confused: :confused:


--
Celtic_Avenger
------------------------------------------------------------------------
Celtic_Avenger's Profile:

http://www.excelforum.com/member.php...o&userid=14101
View this thread: http://www.excelforum.com/showthread...hreadid=261256





All times are GMT +1. The time now is 03:24 PM.

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