我正在尝试将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
答案 0 :(得分:3)
也许您可以使用类似下面的代码。
如果您远离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