选择范围内的范围 [英] selecting a range within a range
问题描述
我使用以下VBA代码(MS Excel 2010)来选择给定范围内的单元格范围,以将复制的单元格复制并插入到源范围中:
范围从行中的工作表中开始2,到第2200行,从第50列改为65。
设置rngFEA = shtTarget.range(myrange)
iMaxLines = 20
使用rngFEA
.Range(Cells(3,1),Cells(3 + iMaxLines,.Columns.Count))。复制
.Range(Cells(3,1)单元格(3 + iMaxLines,.Columns.Count))插入Shift:= xlDown
结束与
这样做(没有参考单元格(行,列)参数的rngFEA)工作正常,所选单元格是所期望的范围的一部分。
我不喜欢使用没有参考的cells()参数,因为使用没有引用使细胞引用工作表,而是给出错误的结果,所以我宁愿使用rngFEA.cells():
设置rngFEA = shtTarget.range(myrange)
iMa xLines = 20
使用rngFEA
.Range(.Cells(3,1),.Cells(3 + iMaxLines,.Columns.Count))。复制
.Range(.Cells(3 ,1),.Cells(3 + iMaxLines,.Columns.Count))。插入Shift:= xlDown
结束与
但是,由此产生的范围远远超出了rngFEA的范围,在某个地方向左和向下。
我甚至找不到所使用的索引和结果偏移量之间的关系。
我相信range.insert也可以表示为
rngFEA.cells(3,1).insert shift:= xldown
但是现在不是我的关心。
我很清楚引用的不同,但是我不明白为什么不使用该范围的引用给出正确的结果,并且使用引用不会。
我希望
rngFEA.range(rngFEA.cells(1,1),rngFEA.cells(10,10))
pre>
将给定范围的最上面,最左侧的单元格范围向下和向下和向下移动到相同范围内。
在示例代码中,我选择指定范围内第3行中最左侧的单元格,直到第23个单元格和范围的右端。
(实际选择的行是工作簿中的第3行,因此范围内的第2行)
我查看了Microsoft信息和几个论坛,但是找不到足够描述此效果的解释。 p>
我知道
range.row
返回范围开始的工作表中的行数,
range.column
返回范围开始的列。
选择指定范围内的单元格或行
范围.row(2)
不返回范围的第二行,但工作表的第二行。一个
为每个myrow在range.rows
索引
myrow.row
返回范围内的行数,但使用它作为选择索引似乎返回工作表中的行,所以我需要添加
range.row + myrow.row
索引到该范围内的实际行。
此后面的机制和上述行为选择范围内的行为范围让我感到困惑
由于有很多方法可以使用VBA来处理Excel中的事情,所以希望您能给出我所描述的行为的一般解释,而不是一个解决方案(如果不是为了解释为什么):)
THX提前
Ydalir
解决方案可以确认此行为:
Sub Tester()
Dim rng As Range
设置rng =范围(C3:H28)
'选择E5:F6(???)
用rng
.Range(。单元格(1,1),.Cells(2,2))。选择
结束
'选择C3:D4(expected)
用rng
rng.Parent.Range(.Cells(1,1),.Cells(2,2))。选择
结束
End Sub
似乎可能与使用
.Range $的双相关组合有关c $ c>和
.Cells
而是使用
rng.Parent.Range
并且只有.Cells
相对于包含范围似乎修复它(并且仍然允许完全限定的范围引用)I am using following VBA code (MS Excel 2010) to select a range of cells within a given range, to copy and insert the copied cells back into the source range: The range starts in the worksheet at row 2, down to row 2200 and from column 50 to 65.
Set rngFEA = shtTarget.range("myrange") iMaxLines = 20 With rngFEA .Range(Cells(3, 1), Cells(3 + iMaxLines, .Columns.Count)).Copy .Range(Cells(3, 1), Cells(3 + iMaxLines, .Columns.Count)).Insert Shift:=xlDown End With
Doing it this way (no reference to rngFEA for the Cells(Row,Column) parameter) works fine, the selected cells are part of the range as expected.
I don't like use no reference for the cells() parameter, because using no reference makes the cells referring to the worksheet and rather give erroneous results, so I would rather like to use rngFEA.cells():
Set rngFEA = shtTarget.range("myrange") iMaxLines = 20 With rngFEA .Range(.Cells(3, 1), .Cells(3 + iMaxLines, .Columns.Count)).Copy .Range(.Cells(3, 1), .Cells(3 + iMaxLines, .Columns.Count)).Insert Shift:=xlDown End With
BUT the resulting range lies well OUTSIDE the range rngFEA, somewhere off to the left and down. I even could not find a relation between the used indexes and the resulting offset.
I believe the range.insert could also be stated as
rngFEA.cells(3,1).insert shift:=xldown
but this is not my concern right now.
I am well aware of the difference of referencing or not, but i do not understand why NOT using the reference to the range gives a correct result, and using the reference does not.
I expect
rngFEA.range(rngFEA.cells(1,1), rngFEA.cells(10,10))
to return the range of the topmost, leftmost cell of the given range down to the tenth cell to the right and down in the same range. In the example code I select the leftmost cell in row 3 within the given range down to the 23rd cell and the right end of the range. (the actually selected row is row 3 in the workbook and hence row 2 in the range) I looked into Microsofts Informations and several forums, but could not find an explanation which describes this effect sufficiently.
I know that
range.row
returns the number of the row in the WORKSHEET where the range starts,
range.column
returns the column where the range starts.
Selecting a cell or a row within the given range
range.row(2)
does not return the second row of the range but the second row of the worksheet. A
for each myrow in range.rows
the index
myrow.row
returns the number of the row within the range, but using it as the selecting index seems to return the row within the worksheet, so I need to add
range.row + myrow.row
to index to the actual row within the range.
The mechanics behind this and the above described behaviors of selecting a range within a range are confusing to me. Since there are many ways to handle things in Excel using VBA, I hope you can give me a general explanation of the described behavior rather than a solution (if not for explaining the why) :)
THX in advance
Ydalir
解决方案Can confirm this behavior:
Sub Tester() Dim rng As Range Set rng = Range("C3:H28") 'This selects E5:F6 (???) With rng .Range(.Cells(1, 1), .Cells(2, 2)).Select End With 'This selects C3:D4 (expected) With rng rng.Parent.Range(.Cells(1, 1), .Cells(2, 2)).Select End With End Sub
Seems like it may be related to the "double relative" combination of using both
.Range
and.Cells
Instead using
rng.Parent.Range
and having only the.Cells
be relative to the containing range seems to fix it (and still allows for fully-qualified range references)这篇关于选择范围内的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!