简体   繁体   中英

VBA - is it possible to call subs or functions from a collection or other storage location?

Pretty self explanatory, I have a set of subs/functions which I'm going to be calling a lot. I would like to be able to store them in a collection or something equivalent so I can just write, for instance:

for counter = 1 to 10
    call MetricSubs(counter)
next counter

Is this possible?

If your always going to be calling the same methods in the same order, the correct way is to call a method that calls your methods:

Public Sub OuterMethod()
    ' do some stuff
    Call InnerMethod()

End Sub

Public Sub InnerMethod()

    Call MethodOne()
    Call MethodTwo()
    Call MethodThree()

End Sub

If there is a change in the inclusion or order of methods to be called, I'm guessing it's predictable and a limited set. Good functional programing will have a parent method for each of these cases. Your code will be much easier to read and maintain.

Public Sub OuterMethod()

    If widget.isRed Then
        Call RedWidgetStuff()
    ElseIf widget.isBlue Then
        Call BlueWidgetStuff()
    Else
        Call NoWidgetStuff()
    EndIf

End Sub

Public Sub RedWidgetStuff()

    Call SetWidgetColor(Red)
    Call PaintWidget()
    Call FinishWidget()

End Sub

Public Sub BlueWidgetStuff()

    Call SetWidgetColor(Blue)
    Call PaintWidget()
    Call FinishWidget()

End Sub

Public Sub NoWidgetStuff()

    Call FinishWidget()

End Sub

If you still think you need to load functions into a collection, I'm going to ask you to seriously question your approach. Better programmers would grimace at the thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM