ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Public array (https://www.excelbanter.com/excel-programming/375309-public-array.html)

bobbo

Public array
 
I have an two event handler procedures that uses a public array
declared in a different module. The public array is undimensioned. In
one of the procedures it is dimensioned and given values. The other
procedure uses this array of values set by the first procedure. I need
to use the array in a procedure on a custom add in. When I try to call
values from this array, I get an error . The editor indicates that the
array is empty. Is there a way I can pass the dimensioned array with
values to the procedure in the add in?


Jim Rech

Public array
 
This isn't an array issue, it is a scope issue. Public variables are
visible within their VB project, not in other VB projects. There are two
ways around this I think. One is to set a reference to the project that has
the array in the project that wants to access it. You will first have to
change the name of at least one of the VB projects (from VBAProject) so they
are different.

The other way is to create a function in the project that has the array that
passes the array to the function's caller. And then use the Run method in
the other project to call that function and receive the array back:

In project with array:

Public Arr() As String

Sub SetArray()
ReDim Arr(1 To 2)
Arr(1) = 10
Arr(2) = 20
End Sub

Function PassArray() As Variant
PassArray = Arr
End Function

In the other project:

Sub GetArray()
Dim MyArray As Variant
MyArray = Run("BookWithArray.xls!PassArray")
MsgBox MyArray(2)
End Sub



--
Jim
"bobbo" wrote in message
ups.com...
|I have an two event handler procedures that uses a public array
| declared in a different module. The public array is undimensioned. In
| one of the procedures it is dimensioned and given values. The other
| procedure uses this array of values set by the first procedure. I need
| to use the array in a procedure on a custom add in. When I try to call
| values from this array, I get an error . The editor indicates that the
| array is empty. Is there a way I can pass the dimensioned array with
| values to the procedure in the add in?
|




All times are GMT +1. The time now is 08:25 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com