【问题标题】:Vba highlight double clicked cell with second double click removing formatting from 2 rangesVba 突出显示双击的单元格,第二次双击从 2 个范围中删除格式
【发布时间】:2016-11-24 19:44:17
【问题描述】:

我有两个范围 A1:A4 和 A5:A10。我希望能够在从第一个范围说 A1 双击时突出显示一个单元格,然后当我双击同一范围内的一个单元格说 A2 时,这将取消突出显示 A1 并突出显示 A2。我想对第二个范围做同样的事情,但彼此独立,所以我最终会得到 2 个突出显示的单元格,一个来自每个范围。我目前使用的代码目前只对第二个范围执行此操作:

Public PreviousCell As Range
Public PreviousCell2 As Range

Private Sub Worksheet_BeforeDoubleClick(ByVal target As Range, Cancel As Boolean)

If Not PreviousCell Is Nothing Then PreviousCell.Interior.ColorIndex = xlNone



With target.Interior
    If Not .ColorIndex = xlNone Then
        .ColorIndex = xlNone
    ElseIf Not Intersect(target, Range("A1:A4")) Is Nothing Then
        .ColorIndex = 15
    ElseIf Not .ColorIndex = 15 Then
        .ColorIndex = xlNone
    End If
End With
Cancel = True

Set PreviousCell = target

If Not PreviousCell2 Is Nothing Then PreviousCell2.Interior.ColorIndex = xlNone

With target.Interior
    If Not .ColorIndex = xlNone Then
        .ColorIndex = xlNone
    ElseIf Not Intersect(target, Range("A5:A10")) Is Nothing Then
        .ColorIndex = 15
    ElseIf Not .ColorIndex = 15 Then
        .ColorIndex = xlNone
    End If
End With
Cancel = True

Set PreviousCell2 = target

End Sub

谢谢!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    啊,好的,我现在看到了问题。问题是您将两个先前的单元格值都设置为target,而不管选择的范围如何。您需要将PreviousCellPreviousCell2 更改为目标只有在它们的对应范围发生更改时。所以如果Target在A1:A4之内,那么PreviousCell将被改变,如果Target在A5:A10之内,那么PreviousCell2应该相应地改变。

    【讨论】:

      【解决方案2】:

      如果在Range("A1:A4") 中双击单元格,则清除整个范围的颜色并突出显示目标范围。对Range("A5:A10") 执行相同操作。

      Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
          If Not Intersect(Target, Range("A1:A4")) Is Nothing Then
              Range("A1:A4").Interior.ColorIndex = xlNone
              Target.Interior.ColorIndex = 15
          ElseIf Not Intersect(Target, Range("A5:A10")) Is Nothing Then
              Range("A5:A10").Interior.ColorIndex = xlNone
              Target.Interior.ColorIndex = 15
          End If
      
      End Sub
      

      【讨论】:

      • 感谢您的完美解决方案!
      • 感谢您的检查!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2012-01-10
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多