Code To Execute A Macro When Any One of A Range Of Cells is Selected.

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this vba solution in an effort to trigger code when a cell is selected by a mouse click.

This works wonderfully if the cell is a single cell. In my scenario, I want the user's clickable area to be within a series of contigiuous cells (A1:H1).
This code does not work if the cells are merged. Fair enough.
I can accomplish the same visual effect by using the center across selection horizontal alignment (to define the range of cells the user can click). But, for the single click to work, the user has to select cell A1 exclsusivley. How can I adapt this code so that the user can trigger the code when any of the cells in that range (A1:H1) is clicked?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Stop
    Dim Message As MSG
 
    'check for left-mouse button clicks.
    PeekMessage Message, 0, 0, 0, PM_NOREMOVE
    'if left-mouse clicked on cell A1 run macro
    If Message.Message = 512 Then
    If Selection.Address = Range("A1").Address Then
        MsgBox "You clicked cell: " & Selection.Address
    End If
    End If
 
End Sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about :

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Stop
    Dim Message As MSG
    Dim MyRange As Range
'
    Set MyRange = Range("A1:H1")
'
'   check for left-mouse button clicks.
    PeekMessage Message, 0, 0, 0, PM_NOREMOVE
'
'   if left-mouse clicked on cell F4 run macro
    If Message.Message = 512 Then
        If Not Intersect(Target, MyRange) Is Nothing Then
            MsgBox "You clicked cell: " & Target.Address
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top