akash bondre
akash bondre

Reputation: 63

Not able to declare array as public VBA

When I tried to declare the array as public from the Main Sub() in Sheet1 it's throwing a compile error:

"Arrays not allowed as Public Members of object Modules".

What I am trying to do is declare an array whose elements can be calculated/manipulated from any modules added in VBA project.

Public Segment() As String

Public SegCount As Integer

Sub EDI()
   'calls to subroutines placed in modules
End Sub

Upvotes: 2

Views: 750

Answers (2)

Patrick Lepelletier
Patrick Lepelletier

Reputation: 1654

the usual workaround woud be using a variant.

Public MyArray as Variant

What can go wrong with that ? well a lot , so preferably pass arrays as arguments (to subs byval a() as string , BUT to functions you can only byval a as variant (could be a byref if you get an error))

note : works too (inside a module):

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

note 2 : anything stoping the code would reset the public values....

Upvotes: 2

user2140173
user2140173

Reputation:

Insert a standard coding Module - it will become a Module1 then stick Public Segment() As String on top of that module... such as

enter image description here

Upvotes: 1

Related Questions