![](/img/trans.png)
[英]excel/VBA how to assign the variable length of an array to integer variable
[英]Assign Procedure to Variable in Excel VBA
我需要像以前用其他语言所做的那样,将过程分配给Excel VBA中的变量。 稍后,将使用这些变量来调用相应的过程。 我想避免4 x 30程序的案例说明。
不幸的是,我尝试了所有可以想到的事情,并在网上搜索,但没有任何效果。 因此,我希望有人对此有所关注,并可能会立即看到缺少哪个魔术字。
万一VBA无法使用这种编程,如果有人可以向我确认,将不胜感激。
我简化了代码以更好地关注该问题,并组成了以下基本部分。 这应该允许重现该错误。
预先感谢,Mounty
PC:Win7Enterprise-64 SP1,Excel 365 ProPlus-32(1808)
'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
考虑以下模型:
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
我想让更新的功能正常运行,现在就可以了! 大帮忙!
类模块============
'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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.