0

I want to run a loop that will call a user-defined function which has the exact same name as the element in an array. I have defined 4 different functions by the names "India", "UK", "USA", and "Japan".

Countries = Array(India, UK, USA, Japan)

For i = 0 To UBound(Countries)

'{call the function named "India" when i = 0}  
'{call the function named "UK" when i = 1}  
'{call the function named "USA" when i = 2}  
'{call the function named "Japan" when i = 3}


Next i

Can someone tell me how to achieve this? Please excuse me if the code formatting is incorrect. Thank you.

1

2 Answers 2

2

Consider:

Sub dural()
    For i = 0 To 3
        Select Case i
        Case 0
            Call India
        Case 1
            Call UK
        Case 2
            Call USA
        Case 3
            Call JAPAN
        End Select
    Next i
End Sub

or:

Sub luxation()
    Countries = Array("India", "UK", "USA", "JAPAN")
    For i = 0 To 3
        Application.Run (Countries(i))
    Next i
End Sub

Tested with:

Sub India()
    MsgBox "India"
End Sub
Sub UK()
    MsgBox "UK"
End Sub
Sub USA()
    MsgBox "USA"
End Sub
Sub JAPAN()
    MsgBox "Japan"
End Sub
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use callbyname to call things in class modules (or any other object).

Executes a method of an object, or sets or returns a property of an object.

Syntax

CallByName(object, procname, calltype,[args()])

OR

Call a vbscript stored in a string (ie a macro). Any app can have macros with 6 lines of code. See http://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.