Worksheet_BeforeDoubleClick进行选择

时间:2015-06-22 21:59:39

标签: excel vba excel-vba events

我有以下代码,不适用于选择:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim Rng1 As Range
Dim I As Integer

If Selection.Count > 1 Then

    For Each cell In Selection
        If I = 0 Then
        Set Rng1 = Range(cell.Address)
        Else
        Set Rng1 = Union(Range(cell.Address), Rng1)
        End If
    I = I + 1
    Next cell
    MsgBox "You have selected the range " & Rng1.Address(False, False)
    Rng1.End(xlDown).Offset(0, 1).Activate
Else
    MsgBox "you are in " & ActiveCell.Address(False, False)
    ActiveCell.Offset(1, 0).Activate
End If


End Sub

我尝试选择一系列细胞,然后双击,是否有人知道这是否可能?

1 个答案:

答案 0 :(得分:3)

由于双击会折叠您之前所做的任何多单元格选择,因此您需要跟踪它,然后检查Target是否在其中。

类似的东西:

Dim rng As Range

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not rng Is Nothing Then
        If Not Application.Intersect(Target, rng) Is Nothing Then
            Debug.Print "Clicked in selected range: " & rng.Address()
        Else
            Debug.Print "Cell: " & Target.Address()
        End If
        Set rng = Nothing
    Else
        Debug.Print "No previous range: clicked in " & Target.Address()
    End If
End Sub

'keeping track of the last multi-cell range selected....
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Set rng = Target
End Sub