Excel VBA: how to trigger an event from code?

I have a Worksheet_BeforeDoubleClick event that opens a list in the target cell. I was asked to provide the same functionality with a button instead of (or in addition to) double-clicking.

In the "Click Event" button, I entered:

Call Worksheet_BeforeDoubleClick(Selection,true)

... therefore the button is just a double-click on the cell. It seems to work well, but before I start using this method in my entire project, I would like to know if there are any pitfalls I should be aware of.

What are the best methods for triggering an event, whether from another event or from a standard code module?

+4
source share
1 answer

, , .

, . Selection . .

  • .

enter image description here

,

Private Sub CommandButton1_Click()
    Call Worksheet_BeforeDoubleClick(Selection, True)
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    MsgBox Target.Address
End Sub

, .

enter image description here

WILL , ... , .

, CAN

Private Sub CommandButton1_Click()
    '~~> Check if what the user selected is a valid range
    If TypeName(Selection) = "Range" Then
        Call Worksheet_BeforeDoubleClick(Selection, True)
    Else
        MsgBox "Not a Valid Range"
    End If
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    MsgBox Target.Address
End Sub

, :) Sub @Sam, Button/Worksheet_BeforeDoubleClick.

, , , .

+5

All Articles