调用函数返回数组 - 类型不匹配

时间:2015-06-04 15:30:42

标签: excel vba excel-vba

我有一个自定义类型,如下所示,

Public Type TypeFieldColumn
    iCol As Integer
    dRow As Double
End Type

然后我有以下子例程

Private Sub PopulateWorksheet()

Dim wsTS As Worksheet
Dim clsData As New clsDatabase
Dim rsTS As New ADODB.Recordset
Dim Fields() As TypeFieldColumn

Set wsTS = ThisWorkbook.Sheets(SomeName)
Set rsTS = clsData.SomeMethod()

Fields = FindFactorColumns(rsTS, wsTS)

End Sub

调用下面的函数

Private Function FindFactorColumns(rsTS As ADODB.Recordset, wsTS As Worksheets) As TypeFieldColumn()

Dim i As Integer
Dim index As Integer
Dim FactorName As String
Dim Flds() As TypeFieldColumn

ReDim Flds(1 To rsTS.Fields.Count - 1)

For i = 1 To rsTS.Fields.Count - 1                      
    FactorName = rsTS.Fields(i).Name
    index = MapBloombergIndexToFactorName(FactorName)
    If index > 0 Then FactorName = pMap(index).MapName

    Flds(i).iCol = Application.WorksheetFunction.Match(FactorName, wsTS.Range("1:1"), 0)
 Next

 FindFactorColumns = Flds

 End Function

我在行= FindFactorColumns(rsTS,wsTS)上遇到类型不匹配的运行时错误 - 我不明白为什么会这样做?

1 个答案:

答案 0 :(得分:0)

只有这样才能保存@Rory时间,这是他的答案。

发生类型不匹配是因为在Populate Worksheet Sub中保存为单个工作表的wsTS在FindFactorColumn函数中被声明为多个工作表。

因为在函数调用时出现了错误行,所以@Rory能够通过查看函数中使用的变量类型并将它们保存在Sub中时将它们与这些相同变量的类型进行比较来确定类型不匹配。