如何在子过程中调用VBA函数并将其声明为变量?

时间:2019-07-01 06:34:05

标签: vba

我试图调用一个函数并将其声明为当前子句中的变量。

我尝试将声明更改为“公开”,但我不知道问题出在哪里。

我想将rngResult声明为函数的结果

Dim i As Integer
For i = 1 To GPN.Count

    lookupValue1 = GPN.Rows(i).Value
    lookupValue2 = Email.Rows(i).Value

Dim rngResult As Range

Set rngResult = DualFindMod(lookupValue1, lookupValue2, lookupRange1, lookupRange2)

rngResult.Offset(0, 2).Value = Union(GPN.Rows(i), Email.Rows(i)).Value

Next i

功能代码

Public Function DualFindMod(lookupValue1 As String, lookupValue2 As String, lookupRange1 As Range, lookupRange2 As Range) As Range
'Returns selection of lookupRanges based on matching lookupValues.

'A. Set parameters for subroutine arguments

'B. Find & match lookupValues to lookupRanges, then select all matching ranges

'collMatch is the collection of the matching ranges

    Dim i As Integer
    Dim rngResult As Range

    If collMatch.Count > 0 Then
        i = 1
        Set rngResult = collMatch.Item(1)
        Do While i < collMatch.Count
            Set rngResult = Union(rngResult, collMatch.Item(i + 1))
            i = i + 1
        Loop
    End If

End Function

3 个答案:

答案 0 :(得分:0)

在宏中调用另一个宏时,如果将变量设置为全局变量,则可以在调用它们之后重新使用它们,它们也将在整个宏中携带其值。

几年前,我做了这个工作,在那里我需要一个主要的“ master”宏来调用多个子宏来执行各种功能(根据启动条件,某些宏会被跳过,因此对我而言是有意义的)。

这是如何设置它们的一个很好的例子: How do I declare a global variable in VBA?

答案 1 :(得分:0)

“您没有在函数中返回任何内容。如果添加语句Set DualFindMod = rngResult:这是您想要的吗?请参见stackoverflow.com/a/2781710/7599798”

得到FunThomas

的答复

答案 2 :(得分:0)

我认为您应该在函数中添加值的返回值,如上所述。这里的代码:

Public Function DualFindMod(lookupValue1 As String, lookupValue2 As String, lookupRange1 As Range, lookupRange2 As Range) As Range
'Returns selection of lookupRanges based on matching lookupValues.

'A. Set parameters for subroutine arguments

'B. Find & match lookupValues to lookupRanges, then select all matching ranges

'collMatch is the collection of the matching ranges

    Dim i As Integer
    Dim rngResult As Range

    If collMatch.Count > 0 Then
        i = 1
        Set rngResult = collMatch.Item(1)
        Do While i < collMatch.Count
            Set rngResult = Union(rngResult, collMatch.Item(i + 1))
            i = i + 1
        Loop
    End If
    DualFindMod = rngResult
End Function

其他选择是使用ref参数:

Public Sub DualFindMod(lookupValue1 As String, lookupValue2 As String, lookupRange1 As Range, lookupRange2 As Range, ByRef rngOutput As Range) 
'Returns selection of lookupRanges based on matching lookupValues.

'A. Set parameters for subroutine arguments

'B. Find & match lookupValues to lookupRanges, then select all matching ranges

'collMatch is the collection of the matching ranges

    Dim i As Integer
    Dim rngResult As Range

    If collMatch.Count > 0 Then
        i = 1
        Set rngResult = collMatch.Item(1)
        Do While i < collMatch.Count
            Set rngResult = Union(rngResult, collMatch.Item(i + 1))
            i = i + 1
        Loop
    End If
    Set rngOutput = rngResult 
End Sub

然后这样称呼它:

Dim i As Integer
For i = 1 To GPN.Count

    lookupValue1 = GPN.Rows(i).Value
    lookupValue2 = Email.Rows(i).Value

Dim rngResult As Range

DualFindMod lookupValue1, lookupValue2, lookupRange1, lookupRange2, rngResult 

rngResult.Offset(0, 2).Value = Union(GPN.Rows(i), Email.Rows(i)).Value

Next i