VBA 过滤器函数在从范围创建的数组上引发 'Runtime error 13: type mismatch' 错误

VBA Filter function throws 'Runtime error 13: type mismatch' error on an array created from a range

我正在尝试使用过滤器函数在数组中搜索子字符串,然后创建一个包含该子字符串的新字符串数组。当我尝试时,我收到类型不匹配运行时错误 13 消息。我读过 filter can only be used on a 1-dimensional array and that populating an array with a range automatically creates a 2-dimensional array,但我想我读过如果我转置数组我可以解决这个问题创建它时。我想我可能是错的,因为它似乎仍然不适合我。

在错误出现之前,headerTitles 数组填充了以下字符串:

这都是使用 Excel 2010.

感谢您的帮助。

Sub dateInHeaderTitle()

    Dim ws As Worksheet
    Dim lastCol As Integer
    Dim headerTitles As Variant
    Dim dateTitles As Variant

    Set ws = ThisWorkbook.Worksheets(1)



    With ws
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        headerTitles = Application.Transpose(.Range(.Cells(1, 1), .Cells(1, Lastcol)).Value2)
    End With


    
    
    dateTitles = Filter(headerTitles, "date", , vbTextCompare)
    

    Set ws = Nothing

End Sub

为了获得一维数组,从列范围开始,你应该使用:

headerTitles = Application.Transpose(Application.Transpose(.Range(.cells(1, 1), .cells(1, lastCol)).Value2))`
Debug.Print Join(headerTitles, "|") 'just to see it...