Pass more than one argument

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,913
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
If I want to pass ONE argument to a SubRoutine, I can do this:

Code:
Dim MyArray() as Variant

MyArray() = Array("Apples", "Oranges")

Dim i As Integer

For i = LBound(MyArray(), 1) To UBound(MyArray(), 1)

    Call SomeSub(Arg:=i)

Next i

What if SomeSub requires TWO arguments? How can I define an array, that allows more than ONE argument to be passed?

Thanks
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I suppose I can use a dictionary, which would be key and item but what if SomeSub requires MORE than 2 arguments?
 
Upvote 0
I don't really understand the question. Best guess, use a 2D array or an array of arrays, and iterate each row.
 
Upvote 0
Why would you need an array to pass more than one argument?
 
Upvote 0
Thanks, the 2D array method is probably the easiest, like this:

Code:
    Dim MyArray(1 To 2, 1 To 2) As Variant
    
    MyArray(1, 1) = "Apple"
    MyArray(1, 2) = "Orange"
    MyArray(2, 1) = "Large"
    MyArray(2, 2) = "Small"
    
    Dim i As Integer
    
    For i = 1 To 2
    
        Call SomeSub(Arg1:=MyArray(1, i), Arg2:=MyArray(2, i))
    
    Next I


Using a dictionary, this is what I've got, which also works:

Code:
    Dim DIC As Scripting.Dictionary
    Set DIC = New Scripting.Dictionary
    
    DIC.Add Key:="Apple", Item:="Large"
    DIC.Add Key:="Orange", Item:="Small"
    
    Dim DICElement As Variant
    
    For Each DICElement In DIC
    
        Call SomeSub(Arg1:=DICElement, Arg2:=DIC.Item(DICElement))
    
    Next DICElement
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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