excel VBA(不是 VBScript)101:如何创建和读取多维数组?

发布于 2024-12-05 09:16:44 字数 305 浏览 1 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-12-12 09:16:44

作为@Jeremy 答案的扩展:

您可以重命名多维数组。

DIM arr() as Variant 'or object or class or whatever.  
Redim arr(1 To 1, 1 To 1) 'works 
Redim arr(1 To 3, 1 To 3) 'works, but deletes any existing data
Redim Preserve arr(1 To 3, 1 To 10) 'works and keeps any data
Redim Preserve arr(1 To 10, 1 To 10) 'fails, you can only redm preserve the last dimension

数组、集合(或字典)是否最适合您的应用程序取决于您的应用程序的详细信息

As an extension to @Jeremy 's answer:

you CAN redim multi dimensional arrays.

DIM arr() as Variant 'or object or class or whatever.  
Redim arr(1 To 1, 1 To 1) 'works 
Redim arr(1 To 3, 1 To 3) 'works, but deletes any existing data
Redim Preserve arr(1 To 3, 1 To 10) 'works and keeps any data
Redim Preserve arr(1 To 10, 1 To 10) 'fails, you can only redm preserve the last dimension

Whether Arrays, Collections (or Dictionaries for that matter) are best for your app depends on the details of your application

披肩女神 2024-12-12 09:16:44

打开 Excel

按 Alt + F11

右键单击​​ VBAProject >插入>类

在左侧窗格、属性对话框中指定 VBA 类“Person”的名称 为

Person 类指定一个属性,例如名字

Public FirstName As String

创建第二个类或模块文件,以下是如何创建/访问 People 类的数组:

Public colOfPeople As New Collection

Public Function MakePeople() As String

Dim clsP As New clsPerson
clsP.FirstName = "Jeremy"

colOfPeople.Add (clsP)

End Function

< strong>解决方案 1:为了使其成为多维数组,我将集合设为数组:

Public multiColOfPeople() As New Collection

Public Function MakeMultiPeople() As String

ReDim Preserve colOfPeople(1) 'dimension multi-array collection
Dim clsP As New clsPerson
clsP.FirstName = "Jeremy"
colOfPeople(0).Add (clsP)

Dim clsP1 As New clsPerson
clsP1.FirstName = "Lisa"
colOfPeople(1).Add (clsP1)

End Function

解决方案 2:使用多维数组(无集合)

Public multiArray(3, 3) As New clsPerson

Dim clsP As New clsPerson
'store
multiArray(0, 1) = clsP
'retrieve
clsP = multiArray(0, 1)

编辑 *

要使用第二种解决方案,请参阅克里斯·尼尔森对 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

Public FirstName As String

Create a second class or module file and here is how to create/access an array of the People class:

Public colOfPeople As New Collection

Public Function MakePeople() As String

Dim clsP As New clsPerson
clsP.FirstName = "Jeremy"

colOfPeople.Add (clsP)

End Function

Solution 1: To make this multi-dimensional, I've made the collection an array:

Public multiColOfPeople() As New Collection

Public Function MakeMultiPeople() As String

ReDim Preserve colOfPeople(1) 'dimension multi-array collection
Dim clsP As New clsPerson
clsP.FirstName = "Jeremy"
colOfPeople(0).Add (clsP)

Dim clsP1 As New clsPerson
clsP1.FirstName = "Lisa"
colOfPeople(1).Add (clsP1)

End Function

Solution 2: Using a multi-dimensional array (no collection)

Public multiArray(3, 3) As New clsPerson

Dim clsP As New clsPerson
'store
multiArray(0, 1) = clsP
'retrieve
clsP = multiArray(0, 1)

Edit *

To use the second solution, see chris neilsen's answer for info on ReDim'ing multidimensional arrays

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文