Forum

VBA code to require...
 
Notifications
Clear all

[Solved] VBA code to require double click to run macro assigned to button

4 Posts
4 Users
0 Reactions
246 Views
(@tkerrinstallnet-com)
Posts: 1
New Member
Topic starter
 

Does anyone know the code necessary to make double clicking a button with a macro assigned the default behavior?  I thought Copilot would lead me to the answer but, alas, it did not.

 
Posted : 10/04/2025 4:43 am
Anders Sehlstedt
(@sehlsan)
Posts: 970
Prominent Member
 

Hello,

I have not checked if it works as intended, but this is the reply from Copilot.

Dim LastClickTime As Double

Private Sub CommandButton1_Click()
If Timer - LastClickTime < 0.5 Then
' Run your macro here
MsgBox "Macro executed!"
Else
MsgBox "Please double-click the button."
End If
LastClickTime = Timer
End Sub

 
Posted : 10/04/2025 6:43 am
(@vandaloit)
Posts: 1
New Member
 

if it is a shape in a worksheet try this

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim Shp As Shape
    For Each Shp In ActiveSheet.Shapes
        If Not Intersect(Target, Shp.TopLeftCell) Is Nothing Then
            If Shp.Name= "shpTest"
                RunMyMacro
                Cancel = True
                Exit Sub
            End If
        End If
    Next Shp
End Sub

Let me know if it fits your needs.

Best regards
OS

 
Posted : 11/04/2025 10:09 pm
(@kjbox)
Posts: 69
Trusted Member
 

A Shape does not have a Double_Click property'

I think the only way you are going to get this to work is not to use a button!

Instead you need to Select a range of cells, merge them add a colour fill and whatever text you want.

You can then use that as a "Button", because it is a range not a shape then it will have a Double_Click property.

In the Sheet object module:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim r As Range
Set r = Range("btnRange")

If Not Intersect(Target, r) Is Nothing Then MyMacro
End Sub

And in a Standard module

Sub MyMacro()
    MsgBox "MyMacro running"
End Sub

I am attaching a file with this method working.

 

 
Posted : 19/04/2025 2:39 pm
Share: