VBA:将 Excel 范围读入对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8718065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA: reading Excel range into an object array
提问by rjsoft
How do I read an Excel range into an object array?
如何将 Excel 范围读入对象数组?
To clarify, for this Excel range of 6 cells...
澄清一下,对于这个包含 6 个单元格的 Excel 范围......
John Roberts 56
Sam Alito 52
and this class...
还有这堂课……
Class supremes
Public firstName
Public lastName
Public age
Dim supreme As New supremes
I'd like to read the Excel range into an array of supreme
such that:
我想将 Excel 范围读入这样的数组supreme
:
arr(1).firstName = "John"
arr(2).age = 52
For a standard array, this is done with a single assignment...
对于标准数组,这是通过单个赋值完成的...
arr = range("supremes")
Is there a similar command to populate the object array?
是否有类似的命令来填充对象数组?
回答by Jon49
There isn't any special way to read data into an array object. You just need to roll your own code.
没有任何特殊的方法可以将数据读入数组对象。您只需要推出自己的代码。
dim i as long
dim rData as range
dim vData as variant
set rData=selection
vData=rData
for i=1 to ubound(vdata)
arr(i).FirstName=vdata(i,1)
arr(i).LastName=vdata(i,2)
arr(i).Age=vdata(i,3)
next i