Call Sub using Concatenation

Plotweaver

New Member
Joined
Jan 28, 2018
Messages
23
How do I call a Subroutine using Concatenation?

Example:
I have 9 subs and I want to call one of them. They all begin with: Group_Evens_Down_Slots_wo_ (1 through 9)

Would it be something like this?:
GN = Range("next_slot") ' ' GN = 3 in this case ' '
Call Group_Evens_Down_Slots_wo_ & GN

Obviously this doesn't work. What will? Thanks.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
use only 1 routine , but pass a parameter : Group_Evens_Down_Slots_wo( GN)

Code:
function Group_Evens_Down_Slots_wo( byval pvVal)
select case pvVal
   case 1
   case 2 
end select

end function
 
Upvote 0
use only 1 routine , but pass a parameter : Group_Evens_Down_Slots_wo( GN)

Code:
function Group_Evens_Down_Slots_wo( byval pvVal)
select case pvVal
   case 1
   case 2
end select

end function
Sorry, but this routine only opened a box of macros within the entire workbook. To make it work, I would have to scroll down thru the list until I found the one I wanted. This is not what I wanted. I just want it to run the sub that I've called out. Thanks any way.
 
Upvote 0
Call Group_Evens_Down_Slots_wo_ & GN is never going to compile, but there are other approaches that will work.

VBA Code:
    GN = Range("next_slot") 'GN = 3 in this case
    Select Case GN
        Case 1
            Call Group_Evens_Down_Slots_wo_1
        Case 2
            Call Group_Evens_Down_Slots_wo_2
        Case 3
            Call Group_Evens_Down_Slots_wo_3
        Case 4
            Call Group_Evens_Down_Slots_wo_4
        Case 5
            Call Group_Evens_Down_Slots_wo_5
        Case 6
            Call Group_Evens_Down_Slots_wo_6
        Case 7
            Call Group_Evens_Down_Slots_wo_7
        Case 8
            Call Group_Evens_Down_Slots_wo_8
        Case 9
            Call Group_Evens_Down_Slots_wo_9
        Case Else
            MsgBox "Error - No such subroutine", vbOKOnly Or vbCritical, Application.Name
    End Select
 
Upvote 0
Solution
Perhaps a more generic way. Note the separate function.

VBA Code:
Public Sub Plotweaver()
    Dim GN As String, MyMacro As String
    
    GN = Range("next_slot")  ' ' GN = 3 in this case ' '
    MyMacro = "Group_Evens_Down_Slots_wo_" & GN
    
    If Not RunMacro(MyMacro) Then
        MsgBox "Macro " & MyMacro & " could not be invoked", vbExclamation
    End If
End Sub


Public Function RunMacro(ByVal argMacroName As String) As Boolean
    On Error Resume Next
    Excel.Application.Run argMacroName
    If Err.Number = 0 Then RunMacro = True
End Function
 
Upvote 0
Call Group_Evens_Down_Slots_wo_ & GN is never going to compile, but there are other approaches that will work.

VBA Code:
    GN = Range("next_slot") 'GN = 3 in this case
    Select Case GN
        Case 1
            Call Group_Evens_Down_Slots_wo_1
        Case 2
            Call Group_Evens_Down_Slots_wo_2
        Case 3
            Call Group_Evens_Down_Slots_wo_3
        Case 4
            Call Group_Evens_Down_Slots_wo_4
        Case 5
            Call Group_Evens_Down_Slots_wo_5
        Case 6
            Call Group_Evens_Down_Slots_wo_6
        Case 7
            Call Group_Evens_Down_Slots_wo_7
        Case 8
            Call Group_Evens_Down_Slots_wo_8
        Case 9
            Call Group_Evens_Down_Slots_wo_9
        Case Else
            MsgBox "Error - No such subroutine", vbOKOnly Or vbCritical, Application.Name
    End Select
Thank you for this code. It seems to be a more simplistic way to solve the problem. I will be using this method to call up the subs in the order they are designed to run.
 
Upvote 0
It seems to be a more simplistic way to solve the problem.
Of course you are completely free to use other solutions for your issue, but given your query (Call Sub using Concatenation), my submission is the closest.

The Call statement transfers control to a procedure, which name needs to be known by VBA at compile time. The Run method on the other hand accepts a variable (of the Variant type). This implies that if the name of the procedure to be invoked isn't known yet at compile time, we're able to compose/evaluate that name at run-time.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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