Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

excel - VBA Range variable gone after cut

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

When the tracked cells cease to exist, the Range instance becomes unusable.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...