以前活动的单元格

对于我正在编写的代码,我监视某些单元格区域中的更改以运行函数和私有子目录。 为此,我使用工作表中的交集function。

然而,交叉'testing'的触发器总是我移出我testing的单元格是否通过鼠标点击到不同的单元格或通过光标移动。

我需要的是一种方法来定义一个variables,其中包含我之前select的单元格的地址。

我尝试了下面的代码,但我得到的是错误。

有没有人有一个想法如何做到这一点?

Public xfrLastCell As String Public xfrActiveCell As String Private Sub Worksheet_SelectionChange(ByVal Target As Range) If xfrActiveCell = "" Then xfrActiveCell = ActiveCell.Address xfrLastCell = xfrActiveCell xfrActiveCell = ActiveCell MsgBox xfrLastCell End Sub 

谢谢你们

下面的代码适用于我 – 你的活动单元的分配丢失了一个地址含义活动单元variables总是空白的

 Option Explicit Public xfrLastCell As String Public xfrActiveCell As String Private Sub Worksheet_SelectionChange(ByVal Target As Range) If xfrActiveCell = "" Then xfrActiveCell = ActiveCell.Address xfrLastCell = xfrActiveCell xfrActiveCell = ActiveCell.Address MsgBox xfrLastCell End Sub 

通过这个代码,引用PreviousActiveCell将返回所需的结果:

 Public PreviousActiveCell as Range Private Sub Worksheet_SelectionChange(ByVal Target As Range) Static pPrevious as Range Set PreviousActiveCell = pPrevious Set pPrevious = ActiveCell End Sub 

这在单个工作表内工作。 您是否需要跨越其他工作表和工作簿的上一个单元格?

记住范围而不是范围的地址可能更容易:

 Dim Oldcell As Range Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Oldcell Is Nothing Then Set Oldcell = Target Exit Sub End If MsgBox "New cell is " & Target.Address & vbCrLf & "Old cell was " & Oldcell.Address Set Oldcell = Target End Sub