我有一个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
,而应使用Long
。 Integer
是16位类型,而Long
是其他所有编程语言都认为是integer
的标准32位类型。