arrays - module to module

  • Thread starter Thread starter mike
  • Start date Start date
M

mike

Is it possible to create an array in one module and then
write the array in another module OR does all the activity
have to tale place in one module? Thanks for the help
 
It's possible if you declare the variable as Public in a regular code
module outside a procedure. See "Understanding Scope and Visibility" in
VBA Help.
 
Mike,

What you want is perfectly feasible. You could either declare the array as a
module variable, that is at the start of the module and not in a macro, or
you could pass it ByRef as an argument from one procedure to another, which
passes the variable pointer and allows updating in another procedure.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Hi!

Either you can declare the array as a public variable on the General section of one of the modules and then simply reference the array in both modules since it is a global variable, or you can send it from Sub (or Function) to Sub (or Function) as a parameter. A variant can contain an array so you can allso send the array in a variant parameter but if you know the typw of data you just declare an array without telling the size in the parameter declaration.

Example:

Sub test()
Dim lngArray(10) As Long

PopulateArray lngArray()
PrintArray lngArray()
End Sub

Public Sub PopulateArray(ByRef lngArray() As Long)
Dim lngLowerBound As Long
Dim lngUpperBound As Long
Dim lngCounter As Long

lngLowerBound = LBound(lngArray)
lngUpperBound = UBound(lngArray)

For lngCounter = lngLowerBound To lngUpperBound
lngArray(lngCounter) = lngCounter
Next
End Sub

Public Sub PrintArray(ByRef lngArray() As Long)
Dim lngLowerBound As Long
Dim lngUpperBound As Long
Dim lngCounter As Long

lngLowerBound = LBound(lngArray)
lngUpperBound = UBound(lngArray)

For lngCounter = lngLowerBound To lngUpperBound
Debug.Print lngArray(lngCounter)
Next
End Sub

In this example the three different Subs can be placed in three different modules and still work together.

Good luck!
 
Your solution may work fine in other versions, but in VB 4.0 Professional (an oldie, I know) it objects to arrays, fixed length strings and others being handled in this manner and the manual is virtually useless on this topic. Can you help? What if I want an array to be of a user-defined type? Can I declare the type in the same place

Best regards.----- Ola Lövgren wrote: ----

Hi

Either you can declare the array as a public variable on the General section of one of the modules and then simply reference the array in both modules since it is a global variable, or you can send it from Sub (or Function) to Sub (or Function) as a parameter. A variant can contain an array so you can allso send the array in a variant parameter but if you know the typw of data you just declare an array without telling the size in the parameter declaration

Example

Sub test(
Dim lngArray(10) As Lon

PopulateArray lngArray(
PrintArray lngArray(
End Su

Public Sub PopulateArray(ByRef lngArray() As Long
Dim lngLowerBound As Lon
Dim lngUpperBound As Lon
Dim lngCounter As Lon

lngLowerBound = LBound(lngArray
lngUpperBound = UBound(lngArray

For lngCounter = lngLowerBound To lngUpperBoun
lngArray(lngCounter) = lngCounte
Nex
End Su

Public Sub PrintArray(ByRef lngArray() As Long
Dim lngLowerBound As Lon
Dim lngUpperBound As Lon
Dim lngCounter As Lon

lngLowerBound = LBound(lngArray
lngUpperBound = UBound(lngArray

For lngCounter = lngLowerBound To lngUpperBoun
Debug.Print lngArray(lngCounter
Nex
End Su

In this example the three different Subs can be placed in three different modules and still work together

Good luck!
 
Nick,

Try using a Variant type variable.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



message
Your solution may work fine in other versions, but in VB 4.0
Professional (an oldie, I know) it objects to arrays, fixed
length strings and others being handled in this manner and the
manual is virtually useless on this topic. Can you help? What
if I want an array to be of a user-defined type? Can I declare
the type in the same place?
Best regards.----- Ola Lövgren wrote: -----

Hi!

Either you can declare the array as a public variable on
the General section of one of the modules and then simply
reference the array in both modules since it is a global
variable, or you can send it from Sub (or Function) to Sub (or
Function) as a parameter. A variant can contain an array so you
can allso send the array in a variant parameter but if you know
the typw of data you just declare an array without telling the
size in the parameter declaration.
Example:

Sub test()
Dim lngArray(10) As Long

PopulateArray lngArray()
PrintArray lngArray()
End Sub

Public Sub PopulateArray(ByRef lngArray() As Long)
Dim lngLowerBound As Long
Dim lngUpperBound As Long
Dim lngCounter As Long

lngLowerBound = LBound(lngArray)
lngUpperBound = UBound(lngArray)

For lngCounter = lngLowerBound To lngUpperBound
lngArray(lngCounter) = lngCounter
Next
End Sub

Public Sub PrintArray(ByRef lngArray() As Long)
Dim lngLowerBound As Long
Dim lngUpperBound As Long
Dim lngCounter As Long

lngLowerBound = LBound(lngArray)
lngUpperBound = UBound(lngArray)

For lngCounter = lngLowerBound To lngUpperBound
Debug.Print lngArray(lngCounter)
Next
End Sub

In this example the three different Subs can be placed in
three different modules and still work together.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top