使用excel vba中的数组和函数调用多个宏

时间:2015-12-15 17:58:29

标签: excel vba excel-vba

我无法调用名称存储在字符串数组中的宏。

我附上了代码。

Option Explicit
Option Compare Text

Dim i, Ro As Integer

Public Sub Universal_Macro()

Dim Col(10) As Integer
Dim macro_name(10) As String

Ro = ActiveCell.Row

i = 1

For i = 1 To 10
    Call Mac_Sched(Col(), macro_name())
Next

End Sub

Sub Mac_Sched(Col() As Integer, Internal_Array() As String)
    Cells(Ro, Col(i)).Select
    Call Internal_Array(i)
End Sub

sub Mac_Sched中收到错误。

2 个答案:

答案 0 :(得分:2)

尝试使用 Application.Run

require "mail"

答案 1 :(得分:0)

TBBH,我真的不知道你要完成什么,而且经过大量编辑的代码除了尝试从数组元素派生的字符串调用宏之外,什么都没有显示特定的方法。

Application.OnTime method使用字符串作为要调用的过程的名称。

Option Explicit
Option Compare Text
Dim i As Integer, Ro As Integer

Sub zero()
    Debug.Print "zero: " & i
End Sub

Sub first()
    Debug.Print "first: " & i
End Sub

Sub second()
    Debug.Print "second: " & i
End Sub

Sub third()
    Debug.Print "third: " & i
End Sub

Public Sub Universal_Macro()
    Dim Col() As Integer
    Dim macro_Name As Variant


    macro_Name = Array("zero", "first", "second", "third")
    ReDim Col(UBound(macro_Name))
    For i = LBound(macro_Name) To UBound(macro_Name)
        Col(i) = i
    Next

    Ro = ActiveCell.Row

    For i = LBound(macro_Name) To UBound(macro_Name)
        Call macro_Sched(Col(i), macro_Name)
    Next

End Sub

Sub macro_Sched(c As Integer, internal_Array As Variant)
    Cells(Ro, c + 1).Select '<~~Don't rely on Select! You dont' even reference a worksheet here.
    Application.OnTime Now, internal_Array(c)
End Sub

如果要将参数传递给子子程序,那么某种字符串替换可能会适应这种情况,但子子接收上的细节不明显。

有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros