双击单元格跳转边缘(如何禁用?)

时间:2016-10-13 20:15:37

标签: excel vba excel-vba

代码采用活动单元格,如果它在适当的列中,并且值为“YES”,则运行两个代码之一(取决于它是摘要还是单个值)。这是在不在表或数据透视表中的“原始”数据上完成的。

大约一半的时间我双击该表中的任何单元格,它将我的活动单元格跳转到单元格范围的顶部或底部。是什么造成的?我该怎么做才能解决它?

在“是”和“否”单元格上都会发生这种情况。

编辑:想出来。这是一个我不知道的excel快捷方式(我没有在excel上使用鼠标)。双击单元格边框会跳转到该部分的顶部或底部。在VBA中是否有一种方法可以为特定工作表禁用此“功能”。我似乎无法在谷歌上找到任何有关它的信息......

编辑2:找到这个:https://superuser.com/questions/610805/disable-navigate-to-end-of-list-when-double-clicking-on-border-of-selected-cell 但我不想在工作表中禁用拖放操作,而我想用VBA执行此操作。

Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Value <> "YES" Then
       Exit Sub
   Else
      If Target.Column <> 11 Then
          Exit Sub
     Else
Cancel = False
Dim j As String
Dim k As String
Dim i As Range
Application.ScreenUpdating = False
    Set i = Target
    k = i.Offset(0, -7).Value 'First value for filter
         Worksheets("Comments").Activate
        If Worksheets("Comments").AutoFilterMode = True Then
             Worksheets("Comments").AutoFilterMode = False
        End If
If i.Offset(-1, 0).Value = "Comments" Then
     j = i.Offset(-1, -9).Value
     Worksheets("Comments").Range("C2").AutoFilter Field:=3, Criteria1:=j

Else
    j = i.Offset(0, -9).Value
    Worksheets("Comments").Range("C2").AutoFilter Field:=4, Criteria1:=k
    Worksheets("Comments").Range("C2").AutoFilter Field:=3, Criteria1:=j
End If
Application.ScreenUpdating = True
Worksheets("Comments").Range("A1").Activate

        End If
        End If
Cancel = True
End Sub

1 个答案:

答案 0 :(得分:0)

首先,让我组织你的代码,它不是很容易阅读。希望这有效,我现在无法尝试。

Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim j As String
    Dim k As String
    Dim i As Range
    Application.ScreenUpdating = False
    Cancel = False
    Set i = Target

    If Target.Value <> "YES" and Target.Column <> 11 Then
       Application.ScreenUpdating = True
       Exit Sub
    Else
        Application.CellDragAndDrop = False 
        k = i.Offset(0, -7).Value 'First value for filter
        Worksheets("Comments").Activate

        If Worksheets("Comments").AutoFilterMode = True Then
             Worksheets("Comments").AutoFilterMode = False
        End If

        If i.Offset(-1, 0).Value = "Comments" Then
             j = i.Offset(-1, -9).Value
             Worksheets("Comments").Range("C:C").AutoFilter Field:=3, Criteria1:=j
        Else
             j = i.Offset(0, -9).Value
             Worksheets("Comments").Range("C:C").AutoFilter Field:=4, Criteria1:=k
             Worksheets("Comments").Range("C:C").AutoFilter Field:=3, Criteria1:=j
        End If

        Application.ScreenUpdating = True
        Worksheets("Comments").Range("A1").select

     End If
     Application.CellDragAndDrop = true
     Cancel = True

End Sub