简体   繁体   中英

Assign Procedure to Variable in Excel VBA

1.Intro

I need to assign procedures to variables in Excel VBA as I did it in other languages before. Later on these variables shall be used to call the corresponding procedure. I'd like to avoid case instructions for 4 x 30 procedures.
Unfortunately I tried everything I could think of and searched the web but nothing worked. So I hope someone can have an eye on it and will probably immediately see which magic word is missing.
In case this kind of programming is not available with VBA I would be thankful if someone can confirm this to me.
I simplified my code for better focusing on the problem and composed the essential part below. This should allow to reproduce the error.

Thanks in advance, Mounty

2. Infrastructure

PC: Win7Enterprise-64 SP1, Excel 365 ProPlus-32 (1808)

3. Code

Class Module

'in StepClass_Module
Public proc As Variant          'proc = procedure to run  

Programming Module

Public step(1) As StepClass_Module      'Declare array of procedures  

Sub main()  
   Set step(0) = New StepClass_Module  
   Set step(1) = New StepClass_Module  

   Set step(0).proc = Import()         'Should allocate corresponding Procedure but runs it => error 13  
   Set step(1).proc = Prepare()        'Should allocate corresponding Procedure but runs it => error 13  

   Run step(0).proc                    'Run corresponding Procedure  
   Run step(1).proc                    'Run corresponding Procedure  
End Sub


Function Import() As Variant  
   Debug.Print ("Import")  
End Function

Function Prepare() As Variant  
   Debug.Print ("Prepare")  
End Function

Consider the following model:

Sub main()
    Dim s As String
    s = "inputt,makereport,outputt"
    arr = Split(s, ",")

    For Each a In arr
        Run a
    Next a
End Sub

Sub inputt()
    MsgBox "input"
End Sub

Sub makereport()
    MsgBox "reporting"
End Sub

Sub outputt()
    MsgBox "dun"
End Sub

Here comes the updated code as I wanted to have it functioning and now it works! Big help!

Class Module ============

    'in StepClass_Module  
    Public proc As String          'proc = procedure to run  

Programming Module
==================

Public step(1) As StepClass_Module      'Declare array of procedures  

Sub main()  
   Set step(0) = New StepClass_Module  
   Set step(1) = New StepClass_Module  

   step(0).proc = "Import"         'allocate corresponding Procedure  
   step(1).proc = "Prepare"        'allocate corresponding Procedure  

   Run step(0).proc                'Run corresponding Procedure  
   Run step(1).proc                'Run corresponding Procedure  
End Sub  



Sub Import()  
   Debug.Print ("Import")  
End Sub  

Sub Prepare()  
   Debug.Print ("Prepare")  
End Sub  

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