2010 年至 2016 年间的 VBA 问题+

VBA issue between 2010 and 2016+

提问人:VBA Beginner 提问时间:10/14/2023 最后编辑:GSergVBA Beginner 更新时间:10/14/2023 访问量:69

问:

我们不得不使用旧版 2010 版的 Excel 来运行一些旧的宏。有人可以告诉我这个宏有什么问题,它在 2010 版本中运行良好,但在 2016 版本中运行良好?我在下面以粗体显示的行中收到不匹配类型错误。我猜这是语法的变化,但我刚刚参加的 1700 美元的 Excel VBA 课程仍然没有回答我的问题。谢谢

Private Function GetDates() As Variant
    Dim strSQL As String
    Dim rs As New ADODB.Recordset
    Dim lstDates() As Date
    
    strSQL = " SELECT DISTINCT(DATE) " & _
             " FROM [" & EXTRACTED_FILENAME & "$] "
    
    Call GetResultSet(rs, cn, strSQL)
    
    If (rs.RecordCount > 0) Then
        ReDim lstDates(rs.RecordCount - 1) ' <== Error here
        Do While (Not rs.EOF)
            lstDates(rs.AbsolutePosition - 1) = CDate(rs.Fields(0))
            rs.MoveNext
        Loop
    End If
    GetDates = lstDates
End Function

这只是一个非常大的提取宏的一个函数,但这是停止整个宏的第一个问题。

VBA Excel-2016 类型不匹配

评论

2赞 GSerg 10/14/2023
您确定这是特定的类型不匹配错误,并且恰好在这一行上吗?的值是多少?您的 Excel 2016 是 64 位吗?您引用的 ADODB 的确切版本是什么?你为什么不使用那个循环?rs.RecordCountrs.GetRows
1赞 Tim Williams 10/14/2023
似乎如果运行没有错误,那么也应该成功。If (rs.RecordCount > 0) ThenReDim lstDates(rs.RecordCount - 1)

答: 暂无答案