过程定义可以嵌套在Excel VBA中吗?

时间:2018-12-17 02:55:23

标签: excel vba

是否可以在VBA中嵌套过程定义,以便内部Sub可以访问外部Subs变量?

类似...

Sub Outer()

    Dim dataSheet As Worksheet
    Dim sheetName As Variant
    Dim sel As Range
    Dim destCol As Variant
    Dim defCol As String
    Dim outerLoop As Boolean

    Sub Inner()

        ' Can I do this and get access to the outer variables in here?
        ' So that I can pass them to the Sub call in here and have that
        ' Sub call access the outer variables?

        dataSheet.Activate

        Set sel = dataSheet.ListObjects("ReqMarkerTable").ListColumns("Quantity").DataBodyRange
        destCol = GetInputBoxVal("Enter destination column:", "Column?", defCol)

        If destCol <> False Then
            Call DuplicateValsByQty(sheetName, destCol, sel)
            outerLoop = False
        End If

    End Sub

End Sub

Sub DuplicateValsByQty(sheetName As Variant, destCol As Variant, sel As Range)

    ' I want this Sub's call within the Inner Sub to have access to the outer variables.

End Sub

我想尽可能避免使用全局变量。

最终,我希望有几个这样的“内部”潜艇以不同的方式处理相同的数据。

或者是否可以通过某种方式从另一个Sub中访问数据?像JS中的点符号一样?

也许我看错了吗?

我们将不胜感激。

0 个答案:

没有答案