当我尝试从 Sheet1 中的 Main Sub()
声明该数组为公共(public)数组时,它会抛出编译错误:
"Arrays not allowed as Public Members of object Modules".
我想要做的是声明一个数组,其元素可以从 VBA 项目中添加的任何模块进行计算/操作。
Public Segment() As String
Public SegCount As Integer
Sub EDI()
'calls to subroutines placed in modules
End Sub
最佳答案
通常的解决方法是使用变体。
Public MyArray as Variant
这会出什么问题吗?很多,所以最好将数组作为参数传递(到 subs byval a() as string
,但对于函数,你只能 byval a as variant
(如果出现错误,可能是 byref
))
注意:也可以(在模块内):
Option Explicit
Public rott() As String 'for a dynamyc array
'Public rott(1 to 10) As String 'for a fixed size array
Sub ff()
ReDim rott(1 To 10) 'or redim preserve rott (1 to 10) if you want to keep values (note you can only redilm the last dimension of the array).
Debug.Print rott(1)
End Sub
注释 2:任何停止代码的操作都会重置公共(public)值....
关于arrays - 无法将数组声明为公共(public) VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26757875/