在Excel VBA中将过程分配给变量

问题描述 投票:1回答:2

1.Intro

我需要在Excel VBA中为变量分配过程,就像之前在其他语言中那样。稍后,这些变量将用于调用相应的过程。我想避免4 x 30程序的案例说明。 不幸的是,我尝试了所有我能想到的东西并在网上搜索但没有任何效果。所以我希望有人可以关注它,并且可能会立即看到缺少哪个神奇的词。 如果这种编程不适用于VBA,我会很感激有人可以向我确认这一点。 我简化了我的代码,以便更好地关注问题,并构成了下面的基本部分。这应该允许重现错误。

先谢谢,Mounty

2. Infrastructure

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

3. Code

课程模块

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

编程模块

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
arrays excel class procedure assign
2个回答
0
投票

考虑以下模型:

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

0
投票

这是更新的代码,因为我想让它运行,现在它的工作原理!大帮忙!

课程模块============

    '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  
© www.soinside.com 2019 - 2024. All rights reserved.