Using Arrays VBA

AD_Taylor

Well-known Member
Joined
May 19, 2011
Messages
687
Is it possible to pass the entire contents of an array as a parameter when you call a Sub/Function?

E.g. I have an array with 5 elements in Sub CallThis()

I want to call another Sub, Sub DoThis().

To run properly DoThis() needs to know all 5 elements in the array. It would be ideal if DoThis could accept it and use it in an array format as well.

Thanks,
Adam
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If DoThis always requires five parameters then one way to do this would be


Code:
Sub CallThis()

    'do stuff

    DoThis arrTest(0), arrTest(1), arrTest(2), arrTest(3), arrTest(4)

    'do stuff

End Sub

Sub DoThis(Arr1 As Variant, Arr2 As Variant, Arr3 As Variant, Arr4 As Variant, Arr5 As Variant)

    '
    'do stuff
    '

End Sub
 
Upvote 0
Just pass the array:
Code:
Sub CallThis()
   Dim arr(1 To 5)
   Dim n As Long
   For n = 1 To 5
   arr(n) = n
   Next n
   DoThis arr
End Sub
Sub DoThis(arr())
   Dim n As Long
   Dim strOut As String
   For n = LBound(arr) To UBound(arr)
      strOut = strOut & vbCrLf & arr(n)
   Next n
   MsgBox Mid(strOut, 2)
End Sub
 
Upvote 0
Thanks for the reply,

Thats how I have it set up at the moment but I would have liked it to be more dyanmic.

As a bit of background, its for a username/password function.

The first Sub sets an array of usernames, then the function checks these against the username entered by the user and the password again entered by the user.

The problem currently is if I add a new username, then I have to add a new parameter to the function and add an extra check to the function to check this parameter.

It'd be great if I could just do a For LBound(userNames) To UBound(userNames) within the function...

EDIT: Didn't see Rorys reply - I think I tried this and it didn't work...

I'll give it another go and get back to you :)
 
Upvote 0
If you are checking for valid usernames from a list, why don't you use a (hidden) worksheet for this?

You can just use a lookup against the username range.
 
Upvote 0
Just pass the array:
Code:
Sub CallThis()
   Dim arr(1 To 5)
   Dim n As Long
   For n = 1 To 5
   arr(n) = n
   Next n
   DoThis arr
End Sub
Sub DoThis(arr())
   Dim n As Long
   Dim strOut As String
   For n = LBound(arr) To UBound(arr)
      strOut = strOut & vbCrLf & arr(n)
   Next n
   MsgBox Mid(strOut, 2)
End Sub

Thanks for this Rory,
Works fine :)

I think the problem I was having before was caused by me creating my arrays explicitly as strings.
 
Upvote 0
I think the problem I was having before was caused by me creating my arrays explicitly as strings.

Hi Adam

That should not be the problem. YOu can pass an array of strings if you want.

Ex.:

Code:
Sub CallThis()
   Dim sArr(1 To 5) As String
   Dim n As Long
 
   For n = 1 To 5
       sArr(n) = VBA.Array("a", "b", "c", "d", "e")(n - 1)
   Next n
   DoThis sArr
End Sub
 
Sub DoThis(arr() As String)
   Dim n As Long
   Dim strOut As String
 
   For n = LBound(arr) To UBound(arr)
      strOut = strOut & vbCrLf & arr(n)
   Next n
   MsgBox Mid(strOut, 2)
End Sub


Remark: this may not work in old versions in the Mac
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top