Search code examples
excelvbaloopscall

Simple repeat or loop Call sub in VBA


I am wondering if there is a simpler way to "loop" a sub to do a number of times.

I would like to do the following:

 Sub defined()
   'code here to be looped
  End sub

 Sub use()
 ... 'previous codes
 Call defined 'do defined once
 ... 'interlude codes
 Call defined(3) 'do defined action thrice
 ... 'interlude codes
 Call defined(2) 'do defined twice
 End Sub

Is there something like that exist? Or I must use Loop, something I still am a little confused on how to code when applying this method with a call sub.

Thank you for your time. edited for tags


Solution

  • You could do something like this:

    Sub Defined(numCalls as Integer)
    Dim i as Integer
    If not numCalls > 0 Then Exit Sub
    For i = 1 to numCalls
    
    'The rest of your code
    
    
    Next
    
    End Sub
    

    This will allow you to do:

    Call defined(3)
    

    Etc.