我在Excel VBA中有一个运行SQL查询的模块,然后转换数组以使其正确定位
With connRSet
.ActiveConnection = conn
.CursorLocation = 3
.CursorType = 2
.Source = "SELECT DISTINCT LEFT(number, 6), shippingStateID, plant " _
& "FROM manufacturing.shippingjob " _
& "WHERE (" & orders & ") AND shippingStateID != 3 " _
& "ORDER BY shippingStateID, number, plant"
.Open
End With
'Placing recordset in array then transposing it for correct orientation
Dim detailArray As Variant
detailArray = connRSet.GetRows
Dim trueArray As Variant
trueArray = TransposeArray(detailArray)
查询运行正常并且可以输出结果集,只是以错误的方式定向,因此TransposeArray函数翻转记录集。应该注意的是,我已经在其他程序中使用过这个功能,而且它已经顺利运行了。
Function TransposeArray(v As Variant) As Variant
' Transpose dimensions of a 0-based
' multi-dimensional array
Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
Dim tempArray As Variant
Xupper = UBound(v, 2)
Yupper = UBound(v, 1)
ReDim tempArray(Xupper, Yupper)
For X = 0 To Xupper
For Y = 0 To Yupper
tempArray(X, Y) = v(Y, X)
Next Y
Next X
TransposeArray = tempArray
End Function
然而,在运行代码时,我得到了
运行时错误“458”:变量使用Visual Basic不支持的自动化类型
这发生在
行中tempArray(X,Y)= v(Y,X)
当X = 0且Y = 1时,其中v(Y,X)= 1.在前一次迭代中,v(Y = 0,X = 0)=“152044”。我意识到这些是不同的数据类型,但两个数组都被编码为Variants。在这种情况下,当它在其他情况下,它是否有理由不起作用?