Passing arguments in a loop

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,619
Members
449,238
Latest member
wcbyers

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