简体   繁体   English

VBA Excel如何使用数字定义一组列?

[英]VBA Excel how can I define a set of columns using numbers?

Some of us have proposed great simple ways to define a set of columns in Excel VBA, such as: 我们中的一些人提出了非常简单的方法来在Excel VBA中定义一组列,例如:

.Union(Columns("a"), Columns("b"), Columns("d"))

Those examples are using letters to refer to those columns. 这些示例使用字母来引用这些列。 I need to refer to my columns using numbers, such as in: 我需要使用数字来引用我的专栏,例如:

.Union(Columns(2), Columns(7), Columns(23)). 

The later works great but what if I need to refer to columns using a range of columns such as in: 后者工作得很好,但是如果我需要使用一系列列来引用列,例如:

.Union(columns(2), columns(7), columns(9 to 23)) 

The idea being that I would not have to define the .Union with each column separately, which, in the present example would necessitate 17 columns(x) listing. 这样的想法是,我不必为每列分别定义.Union,在本示例中,这需要列出17个column(x)。

Not an elegant code. 不是一个优雅的代码。 Furthermore, I need to use numbers because the column numbers are determined programmatically and are changing during execution. 此外,我需要使用数字,因为列号是通过编程确定的,并且在执行过程中会不断变化。

Thanks for your input. 感谢您的输入。 Cheers 干杯

您将需要使用RANGE():

.Union(.columns(2), .columns(7), .Range(.columns(9), .columns(23)))

set MyRange = worksheets("mySheet").range( columns(1), columns(5) ) works pretty well. set MyRange = worksheets("mySheet").range( columns(1), columns(5) )效果很好。
It actually select a range of columns from column 1 to column 5. 它实际上选择了从列1到列5的列范围。
Further to that I can then use the Union to join other single columns. 除此之外,我还可以使用联合来联接其他单个列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM