如何转换为处理2D / 3D阵列

时间:2019-11-08 10:03:57

标签: arrays excel vba user-defined-functions

我有一个issue,能够将参数传递到函数中以返回特定值,并且@ja72为我提供了以下解决方案

我想将其扩展为接受2d / 3d数组,他建议我将其发布为新问题以找到解决方案。

有人可以帮忙吗?

下面的代码产生以下调试输出

2 values defined.
ThisWorkbook.Position(0)
First Value
ThisWorkbook.Position(1)
Second Value

它在名为m_position的工作簿中使用私有数组。通过全局属性ThisWorkbook.Position(index)访问内容。

模块中,具有以下代码:

    Option Explicit

    Public Sub Test()    
        If ThisWorkbook.NoValues Then
            ThisWorkbook.FillValues "First Value", "Second Value"
        End If

        Debug.Print CStr(ThisWorkbook.Count) & " values defined."

        Test_Global_Var 0
        Test_Global_Var 1
    End Sub

Public Sub Test_Global_Var(ByVal index As Long)
    ' Part of a UDF
    Debug.Print "ThisWorkbook.Position(" & CStr(index) & ")"
    Debug.Print ThisWorkbook.Position(index)

End Sub

此工作簿中,具有以下代码:

Option Explicit

Private m_position() As Variant

Private Sub Workbook_Open()
    Call DefaultValues
End Sub


Public Property Get Position(ByVal index As Long) As Variant
    Position = m_position(index)
End Property

Public Sub DefaultValues()
    m_position = Array("First", "Second")
End Sub

Public Sub FillValues(ParamArray args() As Variant)
    m_position = args
End Sub

Public Property Get Count() As Long
    Count = UBound(m_position) - LBound(m_position) + 1
End Property

Public Property Get NoValues() As Boolean
    On Error GoTo ArrUndefined:
    Dim n As Long
    n = UBound(m_position)
    NoValues = False
    On Error GoTo 0
    Exit Sub
ArrUndefined:
    NoValues = True
    On Error GoTo 0
End Property

PS。在VBA中,请勿使用Integer,而应使用LongInteger是16位类型,而Long是其他所有编程语言都认为是integer的标准32位类型。

0 个答案:

没有答案