Passing arguments in a loop

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,411
Instead of writing this:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Option Explicit[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Sub Calling()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
    Call Module1.Called(somearg:=1)
    
    Call Module1.Called(somearg:=2)
        
    Call Module1.Called(somearg:=3)
        
End Sub[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Sub Called(ByRef somearg As Integer)[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
    somearg = somearg + 1[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End Sub

I would like to use a loop, as follows:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Option Explicit

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Calling()
    
    Dim ArgArray() As Variant
    
    ArgArray() = Array(1, 2, 3)
    
    Dim i As Integer
    
    For i = LBound(ArgArray(), 1) To UBound(ArgArray(), 1)

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]        Call Module1.Called(somearg:=CInt(ArgArray(i)))

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Next I

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub[/FONT]
[/FONT][FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]

It works but how can I adapt it if Called takes more than one argument?

For example, if Called looked like this:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Called(ByRef somearg As Integer, ByRef somearg2 As Integer)[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
    somearg = somearg2 + 1[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End Sub

how do I change ArgArray in Calling so it passes the args "in pairs"?

Thanks


[/FONT]
<strike></strike>
[/FONT]
<strike></strike><strike></strike>
[/FONT]
<strike></strike>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,526
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
Sub tiredofit()
    Dim Ary As Variant
    Dim i As Long
    
    Ary = Array(1, 2, 3, 4, 5, 6)
    For i = 0 To UBound(Ary) Step 2
        Call test(CLng(Ary(i)), CLng(Ary(i + 1)))
    Next i
End Sub
Sub test(arg1 As Long, arg2 As Long)
    arg1 = arg1 * arg2
    Debug.Print arg1
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,526
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,129,479
Messages
5,636,573
Members
416,925
Latest member
malamutus

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