Quote:
Originally Posted by soroush.kalantari
I added a new worksheet because without this, I didn’t know how to set rng1. Do you know a way to use copied data in this code without adding new worksheet?
|
OK, I understand. It's fine.
Quote:
Originally Posted by soroush.kalantari
Can you tell me why the line “rng.Cells(i).Select” in my code failed? (these questions are just for improving my understanding of vba)
|
Since
rng is a noncontiguous range (as you could see with your
MsgBox rng.Address), it has multiple
areas (each of these areas is separated by a comma in the
.Address), but when you use
rng.cells(i) it in fact only refers to
area 1, and when that area is too small, say it's got only 2 cells, then when you say
rng.cells(3) it refers to a different but clearly defined cell; to demonstrate:
execute this line:
Code:
range("H10:I10, K10").Select
the green cells will be selected, then execute:
Code:
selection.cells(8).select
the cell with the label 8 will be selected where I've inserted the numbering system:
2023-08-06_144659.jpg
Using
For each cll in rng.cells does iterate properly through a noncontiguous range.
Note that with the new code, it does matter how you select the destination range; you will get a different result if you start at the left when dragging, from if you start at the right, as the
MsgBox rng.Address will show!