gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:32:24 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com