excel VBA(不是 VBScript)101:如何创建和读取多维数组?
我使用的是 Excel 2010,并且有一个宏需要将 OData 结果加载到内存数组中。我唯一缺少的部分是如何在 Excel 中实际创建该数组。
如何创建多维数组(或类的数组) Excel 2010?
我唯一的限制是我构建的任何东西都必须独立包含在 XLSX 中。这意味着对 PowerPivot、插件等的依赖已经消失。我认为这样我就只剩下 VBScript 宏了。
我已经在 MSDN、StackOverflow 和 Google 上搜索了几个小时,但找不到如何执行此操作的明确示例。
I'm using Excel 2010 and I have a macro that needs to load OData results into an in-memory array. The only part I'm missing is how to actually create that array in Excel.
How do I create a multi-dimensional array (or an array of a class) in
Excel 2010?
The only constraint I have is that anything I build must be self contained in the XLSX. So that means dependencies on PowerPivot, addins, etc, are out. I think that leaves me with just VBScript macros.
I've searched MSDN, StackOverflow, and Google for hours and can't find a clear-cut example of how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为@Jeremy 答案的扩展:
您可以重命名多维数组。
数组、集合(或字典)是否最适合您的应用程序取决于您的应用程序的详细信息
As an extension to @Jeremy 's answer:
you CAN redim multi dimensional arrays.
Whether Arrays, Collections (or Dictionaries for that matter) are best for your app depends on the details of your application
打开 Excel
按 Alt + F11
右键单击 VBAProject >插入>类
在左侧窗格、属性对话框中指定 VBA 类“Person”的名称 为
Person 类指定一个属性,例如名字
创建第二个类或模块文件,以下是如何创建/访问 People 类的数组:
< strong>解决方案 1:为了使其成为多维数组,我将集合设为数组:
解决方案 2:使用多维数组(无集合)
编辑 *
要使用第二种解决方案,请参阅克里斯·尼尔森对 ReDim'ing 多维数组信息的回答
Open Excel
Press Alt + F11
Right click on VBAProject > Insert > Class
Specify the Name for the VBA class "Person" in the left hand pane, properties dialog
Give the Person class a property eg firstname
Create a second class or module file and here is how to create/access an array of the People class:
Solution 1: To make this multi-dimensional, I've made the collection an array:
Solution 2: Using a multi-dimensional array (no collection)
Edit *
To use the second solution, see chris neilsen's answer for info on ReDim'ing multidimensional arrays