尝试将范围作为参数传递时,VBA编译错误

时间:2017-01-11 07:41:04

标签: excel-vba function vba excel

我在函数中传递两个范围参数。一个工作正常,但第二个是编译错误。

我在这里调用函数 -

Set SourceRange = Sheets("QueryResult").Range("QueryResult")
Set DestinationRange = Sheets("TradeUnderlyingCptyWWRTemplate").Range("CounterpartyName")
Call PopulateDetails(23, SourceRange, DestinationRange, 2, 8)

这是功能:

Function PopulateDetails(SourceColumns As Integer, Srce As Range, Destination As Range, DestinationColums As Integer, DestinationRows As Integer)

Dim CellName As String
Dim a, b, i As Integer

For b = 0 To DestinationRows - 1
    For a = 0 To DestinationColums - 1

        On Error GoTo Err:
        Sheets("TradeUnderlyingCptyWWRTemplate").Select
        Destination.Offset(b, a).Select
        CellName = Destination.Offset(b, a).Name.Name

        For i = 0 To SourceColumns - 1
            If (Destination.Offset(b, a).Name.Name = Srce.Offset(0, i).Value And Destination.Offset(b, a).Value = "") Then
                Destination.Offset(b, a).Value = Srce.Offset(1, i).Value
                Exit For
            End If
        Next
Err:
        On Error Resume Next
        Err.Clear
        CellName = ""
    Next
Next

End Function

然而,它给出了编译错误:

  

byref参数类型不匹配

代码中突出显示SourceRange

当我将函数更改为只有四个参数(删除Source参数)时,它可以正常工作。

1 个答案:

答案 0 :(得分:1)

您需要更改

Dim SourceRange, DestinationRange As Range

Dim SourceRange As Range
Dim DestinationRange As Range

否则它等同于

Dim SourceRange As Variant
Dim DestinationRange As Range