Passing an array to a sub in VBA

W

WOMBAT

I want to pass an array to a subroutine: I tried some
variants such as Call Output(K(*)) (used in some forms of
HP BASIC), but could not find one that worked. Any ideas
anyone?
For t = 1 To NT 'put data in array
For w = 1 To NW
K(t, w) = t * w
Next w
Next t

Call Output_data(Sheet1, K()) 'pass sheet & array to sub
'this syntax is not accepted by VBA
'
End Sub
'
Sub Output_data(Sheet_name, K_temp())
'this should allow various versions of K to be output to
various sheets
For t = 1 To NT
For w = 1 To NW
Sheet_name.Cells(1 + w, 1 + t) = K_temp(t, w)
Next w
Next t
End Sub
 
M

Malcolm Smith

This seems to work for me:

Sub ArrayPost()

Dim K(5, 4) As Long
Dim t As Long, w As Long


For t = 1 To 4
For w = 1 To 4
K(t, w) = t * w
Next w
Next t

ArrayGather K()

End Sub


Private Sub ArrayGather(anArray() As Long)

Debug.Print anArray(4, 2)

End Sub


- Malc
www.dragondrop.com
 

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

Top