vba - 如何从 ActiveCell 分配范围以供将来引用 (VBA)

标签 vba excel

我正在尝试将 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

最佳答案

也许您可以使用类似下面的代码。 最好远离 SelectActiveCell,并尝试尽可能使用合格的 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/

相关文章:

vba - Excel 2013 工作簿在单独的 Excel 实例中通过链接进行通信

vba - 显示对话框以允许用户在 VBA 中选择 Outlook 文件夹

excel - 从文本中查找关键字列表并将它们列在 Excel 的数组中

vba - 在 Excel 2010 中将单元格内容分成单独的行

C# VSTO Excel 2007 - 禁用隐藏/取消隐藏按钮

excel - self 更新电子表格

vba - 从文件夹中的所有工作簿中删除 VBA 代码

vba - 我仅在某些 Excel 2010 版本中收到 'Compile error: Can' t find project or library'。很难测试这个

excel - Excel 中符合条件的 VLOOKUP 函数的 SUM

java - 使用 Apache POI 创建 excel 图表