Pass more than one argument

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,403
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:

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,403
I suppose I can use a dictionary, which would be key and item but what if SomeSub requires MORE than 2 arguments?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,415
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
I don't really understand the question. Best guess, use a 2D array or an array of arrays, and iterate each row.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,263
Office Version
  1. 365
Platform
  1. Windows
Why would you need an array to pass more than one argument?
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,403
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:

Watch MrExcel Video

Forum statistics

Threads
1,126,998
Messages
5,622,097
Members
415,876
Latest member
csibonga2k17

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
Top