编译器抱怨错误的数据类型

时间:2015-05-08 12:26:31

标签: vba excel-vba excel

我已经编写了下面的程序,每次我尝试运行它时编译说Data是错误的数据类型。 ChooseLineItem将范围作为参数,如您所见Data不是范围,则ChooseLineItem将不会被执行。

所以我的问题是如何编译这段代码?

Public Function MakeKeyValPairs(Data As Variant) As Variant()
    Dim LineItem As Integer
    Dim Headers As KeyValStruct

    If TypeName(Data) = "Range" Then
        LineItem = ChooseLineItem(Data)
    Else
        LineItem = 1
    End If
    Headers = FindHeaders(Data)

    MakeKeyValPairs = AssembleAray(Data, Headers, LineItem)
End Function

仅供参考Data被视为变体的原因是因为它必须能够接受范围和数组。

1 个答案:

答案 0 :(得分:3)

试试这个:

Public Function MakeKeyValPairs(Data As Variant) As Variant()
        Dim LineItem As Integer, Headers As KeyValStruct, r as Range

        If TypeName(Data) = "Range" Then
        Set r = Data
        LineItem = ChooseLineItem(r)
    Else
        LineItem = 1
    End If
    Headers = FindHeaders(Data)

    MakeKeyValPairs = AssembleAray(Data, Headers, LineItem)
End Function