在UDF的excel vba中使用类模块属性

时间:2015-04-10 20:16:15

标签: class excel-vba vba excel

我有以下功能可以正常工作:

Function GetAgentEmailWorksheet(AgentObjectId As String)
    Dim specific_agent As clsAgent
    Set specific_agent = New clsAgent

    specific_agent.AgentSheetName = "agentsFullOutput.csv"

    Dim id_array() As Variant
    id_array = specific_agent.AgentIDArray

    Dim email_array() As Variant
    email_array = specific_agent.AgentEmailArray

    GetAgentEmailWorksheet = vlook_using_array(AgentObjectId, id_array, email_array)

End Function

然而,当我将最后一行改为:

GetAgentEmailWorksheet = vlook_using_array(AgentObjectId, specific_agent.AgentIDArray, specific_agent.AgentEmailArray)

我收到以下错误:

Compile error:
Type mismatch:array or user-defined type expected

如果我将第一个参数分出来,它会点亮AgentIDArray(或AgentEmailArray

为什么?

修改

这里有功能vlook_using_array

Function vlook_using_array(target_string As String, _
                    input_array() As Variant, _
                    output_array() As Variant)

    Dim rows_dim As Long
    Dim cols_dim As Integer

    For rows_dim = 1 To UBound(input_array, 1)
        For cols_dim = 1 To UBound(input_array, 2)
            If input_array(rows_dim, cols_dim) = target_string Then
                vlook_using_array = output_array(rows_dim, cols_dim)
            End If
        Next cols_dim
    Next rows_dim

End Function

以下是clsAgent属性:

Public Property Get AgentClientsArray() As Variant
    AgentClientsArray = get_column_array(AgentClientsCol)
End Property

Public Property Get AgentIDArray() As Variant
    AgentIDArray = get_column_array(1)
End Property

Public Property Get AgentEmailArray() As Variant
    AgentEmailArray = get_column_array(AgentEmailCol)
End Property

这里是类模块中的功能:

Private Function get_column_array(col_num As Integer) As Variant
'   create a range out of the used range (of the sheet) in the column specified
'   used to create array properties in the class

    Dim total_rows As Long
    total_rows = Worksheets(Me.AgentSheetName).UsedRange.rows.Count

    Dim target_range As Range
    With Worksheets(Me.AgentSheetName)
        Set target_range = .Range(.Cells(1, col_num), .Cells(total_rows, col_num))
    End With

    Dim target_arr() As Variant
    target_arr = target_range

    get_column_array = target_arr

 End Function

1 个答案:

答案 0 :(得分:0)

这之间存在差异:

Public Property Get AgentIDArray() As Variant
    AgentIDArray = get_column_array(1)
End Property

Public Property Get AgentIDArray() As Variant()
    AgentIDArray = get_column_array(1)
End Property

第一个(你在你的类中)返回一个恰好是数组的Variant,第二个是变体数组

因此,AgentIDArray的返回值与vlook_using_array预期的参数类型不一致