Application.Run and variable number of arguments

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
Hello again guys,

So I have an error handler that formats cells displays a variety of different error messages depending on where the user is in the process:

sSubs is either a string or array of strings containing macro names that will be run.
iPhase and iSteps are for identifying what stage in the process the user is at.
sArgs is either nothing, a variant, an array of variants or an array of arrays of variants.

Usage:

eHandling "Macro1",1,1
eHandling "Macro2",1,2, "Arg1"

eHandling array("Macro3","Macro4"),1,3
eHandling array("Macro5","Macro6","Macro7"),2,1, array(vbnullstring,array("Arg1","Arg2","Arg3"),array("Arg1","Arg2")

It's the last scenario that I'm having trouble getting my head round. My current code is enclosed below:


Code:
Sub eHandling(sSubs As Variant, iPhase As Integer, iStep As Integer,Optional sArgs As Variant = vbNullString)

Dim iSubs as integer
    If IsArray(sSubs) Then
    
        For iSubs = LBound(sSubs) To UBound(sSubs)
            
            If Not sArgs(iSubs) = vbNullString Then
                Application.Run sSubs(iSubs), sArgs(iSubs)
            Else
                Application.Run sSubs(iSubs)
                Application.Run
            End If
        Next iSubs
    Else
        If Not sArgs = vbNullString Then
            Application.Run sSubs, sArgs
        Else
            Application.Run sSubs
        End If
    End If

...more code...

End Sub

The only way I can think of getting round variable numbers of arguments is by having a case statement counting the bounds of the Argument variant. Something like:

Code:
If isarray(sArgs(iSubs)) Then
  Select Case ubound(sArgs(iSubs))
    Case 1: Application.run sSubs, sArgs(iSubs)(0), sArgs(iSubs)(1)
    Case 2: Application.run sSubs, sArgs(iSubs)(0), sArgs(iSubs)(1),sArgs(iSubs)(2)
...etc

Any suggestions or is there something I'm missing?

Thanks in advance :)
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Why not try using ParamArray.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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