Setting passed arguments directly to an array

Psytrese

New Member
Joined
Mar 30, 2016
Messages
13
I'm trying to set passed arguments directly to an array for ease of use. As an example what I currently do is similar to:
Code:
Sub Macro1(arg1 As String, arg2 As String, arg3 As String, arg4 As String)[INDENT]Dim args(1 to 4) as String[/INDENT]
[INDENT]args(1) = arg1[/INDENT]
[INDENT]args(2) = arg2[/INDENT]
[INDENT]args(3) = arg3[/INDENT]
[INDENT]args(4) = arg4

for a = 1 to 4[/INDENT]
[INDENT=2]args(a)....blahblah[/INDENT]
[INDENT]next a[/INDENT]
End Sub

However this seems like a waste of code. What I was hoping to do was something akin to:
Code:
Sub Macro1(args(1) As String, args(2) As String, args(3) As String, args(4) As String)

This would allow me to use these in for loops straight away. Is this possible? It's not a huge deal and what I'm doing works fine but issues mostly arise when there's much more than 4 arguments and when I'm debugging I have to step over these lines multiple times. It can be infuriating.

I'm hoping I'm missing something straight forward, such as a means to refer to arguments directly like "arguments[0]".
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You could just pass one string array to the routine from the caller?
 
Upvote 0
It's not feasible I'm afraid. Much of the data comes from different sources and a lot of it is hard typed so there's no variable to pass.

But if this is the only solution then at least I know I'm not missing anything. Thanks.
 
Upvote 0
I don't really follow that logic. If you have 4 strings to pass, you can add them to an array and pass that.

You could use a ParamArray, but it has to be declared as a Variant type not a String - I don't know if that's important to you.
 
Upvote 0
Setting it to an array prior to passing it is not really any different to setting it after other than the number of arguments I'd be passing. It's not actually any better than what I do currently.

To maybe explain better, as an example, I have a macro designed to sort information with the parameters being the range to sort and then a number of optional columns, e.g:

Sub SortMacro(Sort_Range as Range, SortCol1 as Long, Optional SortCol2 as long, etc, etc...)

I use it in a variety of other macros and since it's specific to them I don't actually need to store the columns in a variable, I just call it like:

Call SortMacro(Sort_Range, 1, 5, 7)

To avoid having to add each sort field individually I re-set the passed variables to an array like then I have a for loop to add them. If they could be received directly to an array I wouldn't need to do this.

Honestly, "no you can't do that" is fine. It just seemed pointless to set an array when I've used other languages which can refer to arguments by argument[1], argument[2], etc, (i.e, already essentially a 1D array) and I was worried I might be missing something.

Thanks for your help.
 
Upvote 0
If you have multiple optional arguments then you do want a paramarray as I said earlier.
 
Upvote 0
Further to Rory's suggestion,

Code:
Sub P()
  SortMacro Range("A:Z"), 1, 5, 7
End Sub

Function SortMacro(r As Range, ParamArray avCol() As Variant)
  Dim i As Long
  
  For i = UBound(avCol) To 0 Step -1
    r.Sort Key1:=r.Columns(avCol(i))
  Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,610
Members
449,174
Latest member
ExcelfromGermany

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