我有一张Excel表单,在单元格“P1”中有一个双击事件(运行一个宏)。
在双击“P1”之前,我可能选择了单元格“J30”(或任何其他单元格) 我怎么能记住,并在“P1”双击后返回“J30”单元?
存储活动单元格不起作用,因为双击顺序中的第一次单击选择“P1”。
我也尝试右键单击“P1”,但在运行事件之前也选择“P1”。
答案 0 :(得分:2)
嗯,它比“重复线程”复杂一点,因为在SelectionChange
事件之前调用了BeforeDoubleClick
事件,所以前者会在最后一次选择之前更新新事件。后者得到了。
您需要的是通过实际保存两者来“更进一步”保存选择:
这样的事情应该有效
' Code module of your worksheet
Option Explicit
Private lastSelection As Range, beforeLastSelection As Range
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' Your Code for this event, i.e.
If Target.Address = "$P$1" Then
' Some code ...
Cancel = True
If Not beforeLastSelection Is Nothing Then beforeLastSelection.Select
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set beforeLastSelection = lastSelection
Set lastSelection = Target
End Sub
答案 1 :(得分:1)
Using the method here您可以按照以下方式执行
Public PreviousActiveCell As Range
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox ("Previous selection: " & PreviousActiveCell.Value & vbNewLine & _
"Double clicked selection: " & Target.Value)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static pPrevious As Range
Set PreviousActiveCell = pPrevious
Set pPrevious = ActiveCell
End Sub