通过函数重用数组
我有两个不同的函数需要访问同一数组(该数组不是常量;只要在工作表的单元格中使用该函数,就会对其进行编辑和附加)。
我想让这个数组可供他们两人使用。该数组需要是多维的(或者是一个可以包含多个元素的 UDT,就像我在下面的代码中尝试的那样),并且需要能够动态调整大小。这是我的一些示例代码(稍作编辑),但它似乎无法正常工作。
Option Base 1
Private Type PathsArray
Nodes() As String
End Type
' Instantiate the global array
Dim Paths(1 To 1) As PathsArray
Function SETTWENTY()
' Increase size of the array, preserving the current elements already inside it
ReDim Preserve Paths(1 To UBound(Paths) + 1)
' Make the inner array be 20 elements long
ReDim Preserve Paths(UBound(Paths)).Nodes(1 to 20)
' Return something random
GETPATH = UBound(Paths)
End Function
Function SETTHIRTY()
' Increase size of the array, preserving the current elements already inside it
ReDim Preserve Paths(1 To UBound(Paths) + 1)
' Make the inner array be 30 elements long
ReDim Preserve Paths(UBound(Paths)).Nodes(1 to 30)
' Return something random
GETPATH = UBound(Paths)
End Function
有人知道为什么这行不通吗?
I have two different functions that require access to the same array (the array isn't a constant; it will be edited and appended to whenever the function is used within a cell in the sheet).
I want to make this array available to both of them. The array needs to be multi-dimensional (or be a UDT that can have multiple elements within it, like I tried in my code below), and it needs to be able to be dynamically resized. Here is some sample code (edited a bit) I have, but it doesn't seem to work properly.
Option Base 1
Private Type PathsArray
Nodes() As String
End Type
' Instantiate the global array
Dim Paths(1 To 1) As PathsArray
Function SETTWENTY()
' Increase size of the array, preserving the current elements already inside it
ReDim Preserve Paths(1 To UBound(Paths) + 1)
' Make the inner array be 20 elements long
ReDim Preserve Paths(UBound(Paths)).Nodes(1 to 20)
' Return something random
GETPATH = UBound(Paths)
End Function
Function SETTHIRTY()
' Increase size of the array, preserving the current elements already inside it
ReDim Preserve Paths(1 To UBound(Paths) + 1)
' Make the inner array be 30 elements long
ReDim Preserve Paths(UBound(Paths)).Nodes(1 to 30)
' Return something random
GETPATH = UBound(Paths)
End Function
Anyone know why this won't work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题的根源是您正在尝试调整“静态”模块级数组的大小。以下是“静态”和“动态”VBA 数组之间差异的详细描述(来自 Chip Pearson):
http://www.cpearson.com/excel/vbaarrays.htm
您还有第二个问题,您的函数将返回 VBA 值
Empty
而不是数量路径。在 VBA 中,可以通过将值分配给函数名称来从函数返回值。在下面的代码中,我通过以下方式解决了这些问题:
您可能并不真正需要(2)如果无论如何,您最初的
(1 To 1)
声明并不是您真正想要的。请注意
Option Explicit
的使用:(3)。如果你在那里,即使在修复(1)之后,带有“GETPATH”分配的原始代码也将无法编译。The root of your problem is that you are trying to resize a "static" module-level array. Here is a good description (from Chip Pearson) of the difference between "static" and "dynamic" VBA arrays:
http://www.cpearson.com/excel/vbaarrays.htm
You have a secondary problem in that your functions will return the VBA value
Empty
instead of the number of paths. In VBA, you return a value from a function by assigning the value to the name of the function.In the code below, I fixed those problems by:
You might not really need (2) if your original
(1 To 1)
declaration wasn't really what you wanted anyway.Note the use of
Option Explicit
re: (3). If you'd had that there, your original code with the "GETPATH" assignments would fail to compile, even after fixing (1).