Passing Array from called Subroutine back to the Subroutine that called it

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
128
Office Version
  1. 2019
Platform
  1. Windows
I have a subroutine named TeamSelection which is too long to post within this forum. However, within it, I call another subroutine named PlayerCombo. PlayerCombo is designed to generate combinations of players (based on groups of 8, 12, 16 or 20 and 4 at a time) AND creates a new array called arrFreqPlay (initially defined variant) that contains numbers reflects how frequently players have been paired up. This helps determine who should and should not get paired up based on how many times players have been paired already.

I would like to be able to pass the arrFreqPlay that was created and populated within the PlayerCombo subroutine back to TeamSelection so additional work can be done with that data.

Not sure if this is important at this point in time but, in this example, within the subroutine named PlayerCombo, the new array called arrFreqPlay is redefined by the following: arrFreqPlay(1 To UBound(lCombinations), 1 To UBound(lPairs), where UBound(lCombinations) equals 70 based on a combination of 8 players, 4 at a time and UBound(lPairs) = 6 (this will always equal 6).

At one time, I was thinking about defining arrFreqPlay as a global array within the module but did not believe that would be the best approach since I may need to use that same array within other subroutines contained within that same module. However, if a new arrFreqPlay array was needed, that array would contain a different set of values based on a new combination of players.

Example: if 12 players are available to play, the following high-level calls are made to subroutines:
  1. Call TeamSelection (procedures to help determine which players will fill court 1)
    1. Call Player Combo (generates list based on 12 players, 4 at a time) and creates arrFreqPlay
    2. Would like TeamSelection to use the results of arrFreqPlay to help identify best combination of 4 to fill first court
    3. Call TeamSelection2 (procedure would be called because 8 players remain to be grouped together)
      1. Call Player Combo (list based on 8 players, 4 at a time) and creates arrFreqPlay (again)
      2. Would like TeamSelection2 to use the results of arrFreqPlay to help identify best combination of the next 4 players to fill second court
      3. After second court is determine, only 4 players remain to fill third and final court so no further "calls" are required.
I hope this gives you enough information to get a feel for how I'm hoping to pass information along from a subroutine back to the procedure that called it. If not, I can try to pare down my program.

Thanks for any help or guidance you can provide.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Declare arrFreqPlay in Teamselection then pass it as a ByRef parameter to PlayerCombo that way changes made to arrFreqPlay by Sub PlayerCombo will be availabe in Teamselection.
VBA Code:
Sub TeamSelection()
    Dim arrFreqPlay As Variant, x As Variant

    PlayerCombo arrFreqPlay

    For Each x In arrFreqPlay
        Debug.Print x
    Next x
End Sub

Sub PlayerCombo(ByRef arrFreqPlay As Variant)
    Dim I As Long

    arrFreqPlay = Array(1, 2, 3)
    
    For I = LBound(arrFreqPlay) To UBound(arrFreqPlay)
       arrFreqPlay(I) = arrFreqPlay(I) * 2
    Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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