通过函数重用数组

发布于 2024-11-25 04:44:06 字数 1021 浏览 2 评论 0原文

我有两个不同的函数需要访问同一数组(该数组不是常量;只要在工作表的单元格中使用该函数,就会对其进行编辑和附加)。

我想让这个数组可供他们两人使用。该数组需要是多维的(或者是一个可以包含多个元素的 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 技术交流群。

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

发布评论

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

评论(1

生寂 2024-12-02 04:44:06

问题的根源是您正在尝试调整“静态”模块级数组的大小。以下是“静态”和“动态”VBA 数组之间差异的详细描述(来自 Chip Pearson):

http://www.cpearson.com/excel/vbaarrays.htm

您还有第二个问题,您的函数将返回 VBA 值 Empty 而不是数量路径。在 VBA 中,可以通过将值分配给函数名称来从函数返回值。

在下面的代码中,我通过以下方式解决了这些问题:

  1. 使模块级数组“动态”,
  2. 添加“init”例程以获取其中的初始元素,
  3. 返回您期望从函数中获得的值

您可能并不真正需要(2)如果无论如何,您最初的 (1 To 1) 声明并不是您真正想要的。

请注意 Option Explicit 的使用:(3)。如果你在那里,即使在修复(1)之后,带有“GETPATH”分配的原始代码也将无法编译。

Option Explicit
Option Base 1

Private Type PathsArray
    Nodes() As String
End Type

' Just declare the module-level array
Dim Paths() As PathsArray

Public Sub init()
    ReDim Paths(1 To 1) As PathsArray
End Sub

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
    SETTWENTY = 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
    SETTHIRTY = UBound(Paths)

End Function

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:

  1. making the module-level array "dynamic"
  2. adding an "init" routine to get your initial element in there
  3. returning the values you expect from your functions

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).

Option Explicit
Option Base 1

Private Type PathsArray
    Nodes() As String
End Type

' Just declare the module-level array
Dim Paths() As PathsArray

Public Sub init()
    ReDim Paths(1 To 1) As PathsArray
End Sub

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
    SETTWENTY = 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
    SETTHIRTY = UBound(Paths)

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