我正在尝试将 ActiveCell 分配给一个变量以便稍后使用它。它给了我一个超时错误。
代码如下,但通常我还没有弄清楚如何动态为变量分配范围。
Sub findCells()
Dim topCell As Integer
Dim left_Cell As Integer
Set refCell = Range(ActiveCell)
refCell.End(xlUp).Select
topCell = ActiveCell.Value
MsgBox topCell
refCell.End(xlToLeft).Select
leftCell = ActiveCell.Value
MsgBox leftCell
End Sub
最佳答案
也许您可以使用类似下面的代码。
最好远离 Select
、ActiveCell
,并尝试尽可能使用合格的 Range
。
Option Explicit
Sub findCells()
Dim topCell As Long
Dim leftCell As Long
Dim refCell As Range
Set refCell = ActiveCell
topCell = refCell.End(xlUp).Value ' <-- gets the value of the top cell
topCell = refCell.End(xlUp).Row ' <-- gets the row number of the top cell
MsgBox topCell
leftCell = refCell.End(xlToLeft).Value ' <-- gets the value of the column to the left
leftCell = refCell.End(xlToLeft).Column ' <-- gets the column number of the column to the left
MsgBox leftCell
End Sub
关于vba - 如何从 ActiveCell 分配范围以供将来引用 (VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42210839/