vba 以前活跃的细胞
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19179756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Previously active cell
提问by user2845354
For the code I am writing I monitor the changes in certain cell ranges to run functions and private subs. For this I use the Intersect function in the worksheet_change
sub.
对于我正在编写的代码,我监视某些单元格范围的变化以运行函数和私有子函数。为此,我在worksheet_change
sub 中使用了 Intersect 函数。
However, the trigger for the intersect 'test' is always that I 'move out' of the cell I am testing for whether it'd be via mouseclick into a different cell or via cursor move.
但是,相交“测试”的触发器始终是我“移出”我正在测试的单元格,以确定它是通过鼠标单击进入不同的单元格还是通过光标移动。
What I need is a way to define a variable which contains the .address
of the cell I had selected before.
我需要的是一种定义变量的方法,该变量包含.address
我之前选择的单元格的 。
I tried the code below, but all I get is errors.
我尝试了下面的代码,但我得到的只是错误。
Does anybody have an idea how to do this?
有没有人知道如何做到这一点?
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
回答by Krishna
below code works for me - your assignment of activecell was missing an Address meaning activecell variable is always blank
下面的代码对我有用 - 您对 activecell 的分配缺少地址,这意味着 activecell 变量始终为空
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
回答by LS_???
With this code, referring PreviousActiveCell
will return desired result:
使用此代码,引用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
This works inside single worksheet. Do you need a previous cell across other sheets and workbooks?
这适用于单个工作表。您是否需要跨其他工作表和工作簿的上一个单元格?
回答by Gary's Student
It might be easier to "remember" the Range rather than the address of the range:
“记住”范围而不是范围的地址可能更容易:
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
回答by ZygD
If
is unnecessary. I have used this:
If
是不必要的。我用过这个:
Public sPreviousTarget As String
Public sTarget As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
sPreviousTarget = sTarget
sTarget = Target.Address
End Sub
Also, Target
is different from ActiveCell
(ActiveCell
refers to one of the cells in Target
).
此外,Target
不同于ActiveCell
(ActiveCell
指 中的单元格之一Target
)。