控制行标题的双击事件

时间:2014-09-05 02:39:46

标签: excel excel-vba double-click vba

所以我非常熟悉引用工作表事件的工作表范围,例如双击。但是在这种情况下,我想要在行标题被双击而不是单元格时引用。它仍然是一个特定的工作表,但到目前为止我还没有成功。

我有多个范围可以在双击时执行不同的事件,因此我使用的代码类似于以下示例:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rWatchRange As Range
    Dim sWatchRange As Range

    Set rWatchRange = Range("A5:A1000")

    'I somehow need it to recognize if the row header is 
    'double clicked between row 5 and 1000 to fire off the second sub

    Set sWatchRange = Range("5:1000")


    If Not Application.Intersect(Target, rWatchRange) Is Nothing Then
        Run "aFormattingSub"
    End If

    If Not Application.Intersect(Target, sWatchRange) Is Nothing Then
        Run "aSubToInsertNewLineAndGroupWithRowAbove"
    End If      
End Sub

我不确定是否有工作表参考,应用程序参考或Excel中我知道的设置可以执行此操作。

1 个答案:

答案 0 :(得分:3)

双击标题时,不会触发DoubleClick事件。我不认为有任何微不足道的方法 - 你必须接受提供的事件。

我认为还有足够的空间来实现更多功能 为了给你一些更多的想法,你可以通过双击或右键单击ctrl按住来做不同的事情。

按住ctrl对右键单击作出反应的示例,仅当选择了整行时:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If (GetKeyState(KeyCodeConstants.vbKeyControl) And &H8000) And _
        Selection.Address = Selection.EntireRow.Address Then

        Cancel = True
        ' ... code
    End If
End Sub

And &H8000只需要对当前存在的内容做出反应并忽略之前的按键

在模块中导入API函数:

Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer