How to convert into handling a 2D/3D array

喜夏-厌秋 提交于 2019-12-24 02:18:36

问题


I have an issue with a being able to pass a parameter into a fucntion to return a specific value and I was given the solution below by @ja72

I wanted to expanded it into accepting a 2d/3d array and he suggested I post it as a new question to find the solution.

Can anyone help?

The code below produces the following debug output

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

It uses a private array in the workbook named m_position. The contents are accessed by a global property ThisWorkbook.Position(index).

In a module have the following code:

    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

In ThisWorkbook have the following code:

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. In VBA never use Integer, but instead use Long. Integer is a 16bit type, while Long is the standard 32bit type that all other programming languages consider as an integer.

来源:https://stackoverflow.com/questions/58764370/how-to-convert-into-handling-a-2d-3d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!