Run a macro when a cell is clicked.

mikeburg

Board Regular
Joined
Jun 23, 2005
Messages
165
I am trying to come up with VBA that will start upon left clicking cell F4 (selecting it with the mouse).

I would appreciate any help I can get. Thanks, mikeburg
 
Thank a bunch. I can't believe I still overlook things so simple.

I just wish there was a way to run the VBA only when mouse selected, not also when selected using the enter or arrow key.s

mikeburg
 
Upvote 0
Here is a nice way to create a cell Mouse Left Click event . Selecting a cell with the keyboard won't trigger the Selection Change Event ! only selecting a cell with the mouse will.

here is a workbook demo : http://www.savefile.com/files/1340448

Place in the worksheet module :

Rich (BB code):
Option Explicit
 
Private Type POINTAPI
    x As Long
    y As Long
End Type
 
Private Type MSG
    hwnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type
 
Private Declare Function PeekMessage Lib "user32" _
Alias "PeekMessageA" _
(ByRef lpMsg As MSG, ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, _
ByVal wRemoveMsg As Long) As Long
 
Private Const PM_NOREMOVE = &H0
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
    Dim Message As MSG
 
    '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 Selection.Address = Range("f4").Address Then
        MsgBox "You clicked cell: " & Selection.Address
    End If
    End If
 
End Sub


Regards.
 
Last edited:
Upvote 0
Many, many thanks to all 3 of you. I am using all 3 in a couple of other situations.

I really appreciate everyone when they think of something else, will take the time to add to the 1st answer!

mikeburg
 
Upvote 0

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