Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

--
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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default 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



--
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identifying a Selected Range in a Macro DCSwearingen Excel Discussion (Misc queries) 4 April 25th 06 04:01 PM
Macro to print a selected range, not entire worksheet James C Excel Discussion (Misc queries) 3 October 19th 05 10:12 PM
TO TOM OR KEN: macro to calculate selected range mary Excel Programming 3 January 19th 04 05:45 PM
Apply a macro to a pre-selected range of cells tim knowles Excel Programming 2 January 13th 04 01:12 PM
Execute a Macro when specific value is selected in a Range Kevin Excel Programming 3 November 21st 03 03:54 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"