剪切后VBA范围变量消失 [英] VBA Range variable gone after cut
问题描述
424 错误.有人能告诉我为什么范围变量在剪切后消失了吗?
424 error. Would anyone be kind enough to tell me why the range variable was gone after cut?
with sheets(1)
Dim des as range
set des = .range("A15")
.range("A1:A3").cut des
msgbox(des.row+5)
end with
推荐答案
Range
对象存储对特定单元格的引用,而不是特定地址.当引用的单元格移动时,Range
对象将跟随.例如.如果您存储 Range("B1")
然后在 A 和 B 之间插入一列,您的变量现在将具有 Range("C1")
,因为您的 B1
已向右移动.
A Range
object stores a reference to particular cells, not to particular address. When the referenced cells move around, the Range
object will follow. E.g. if you store a Range("B1")
and then insert a column between A and B, your variable will now have Range("C1")
, because your B1
has moved to the right.
当被跟踪的单元格不再存在时,Range
实例变得不可用.
When the tracked cells cease to exist, the Range
instance becomes unusable.
剪切完全替换了实际的单元格,而不是简单地覆盖它们的内容,因此您的 Range
正在跟踪的单元格不再存在,而其他一些单元格现在将假定为 A15代码>.这是一个不同的单元格,所以你的
Range
实例消失了.
Cutting replaces the actual cells entirely as opposed to simply overwriting their contents, so the cell your Range
is tracking stops to exist, and some other cell will now assume the address of A15
. That is a different cell though, so your Range
instance is gone.
一个值得注意的例外是您只销毁 Range
的一部分.在这种情况下,Range
接受新单元格并保持其形状.例如.如果您存储 Range("A15:A20")
并在 A15
上剪切 Range("A1:A3")
,则结果范围仍将是 A15:A20
.
A notable exception is when you destroy only a part of the Range
. In that case the Range
accepts the new cells and maintains its shape. E.g. if you stored Range("A15:A20")
and cut Range("A1:A3")
over A15
, the resulting range would still be A15:A20
.
这篇关于剪切后VBA范围变量消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!