使用VBA复制多个范围 [英] Copy multiple ranges with VBA
问题描述
我想使用Excel VBA复制多个范围。我理解,为了选择多个范围,使用类似于下面的语法:
I am trying to copy multiple ranges using Excel VBA. I understand that in order to select multiple ranges, syntax similar to that below is used:
Range("A1:B4000, F1:F4000").Select
在选择 。但是,以下操作:
This works fine in selecting the appropriate range. However, the following:
Range("A1:B4000, F1:F4000").Copy
...仅复制A1:B4000范围。这是我面临的第一个问题。
...only copies the A1:B4000 range. This is the first problem I am facing.
其次,我想动态地将数据复制到底行,这不一定是行#4000。如果选择单个范围,语法如下:
Secondly, I would like to dynamically copy the data to the bottom row, which is not necessarily row #4000. If selecting a single range, the syntax is as follows:
Range("A1", Range("B1").End(xlDown)).Copy
上面的代码成功将A1的所有内容复制到B列的底部。我找不到任何材料在网上解释如何做这个多个选择。
The above code successfully copies everything from A1 to the bottom of the B column. I can't find any material on the net explaining how to do this for multiple selections.
我本质上想做的是复制A1:B(底部)和F1:F(下),但上面两个问题阻止了我。我假设这是一个语法问题。
What I'm essentially trying to do is copy A1:B(bottom) and F1:F(bottom), but the above two issues are stopping me. I assume this is a syntax issue..?
推荐答案
@Scott Holtzman在注释中提供了解决方案:
@Scott Holtzman provided the solution in a comment:
我只是调整以下,因为OP要求动态
范围名称,设置range1 = Range(A1:B& Range (B&
Rows.Count).End(xlUp).Row)以获取列B中的最后一个单元格。
对于列F也相同
I would just adjust the following, since the OP asked for dynamic range names, Set range1 = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row) to get the real last cell in column B. Do the same for column F as well
这篇关于使用VBA复制多个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!