我有一个数组,它存储在字典中,它的边界是从(0到29和0到7)并存储字符串和整数的混合。
我试图在不循环的情况下获取其中的一列,但是每次我都会收到类型不匹配错误。
我发现可以与 application.index 一起使用的数组的大小有限制,但远未接近该限制。
Dim tmp As Variant
' Get Array from Public Dictionary
tmp = FBList(214)
' Output a string of values from one column of array
Debug.Print Join(Application.WorksheetFunction.Index(tmp, 19, 0), ",")
我总是在最后一行得到类型不匹配的结果。我已成功地将其与其他数组一起使用,但这次不行。
Update: Populating FBList
Dim i As Integer, j As Integer, NoCol As Integer, si As Integer, sKey As Integer Set cn = Nothing With FBList .RemoveAll .CompareMode = TextCompare End With With FBMap .RemoveAll .CompareMode = TextCompare End With If UserList.Count = 0 Or ThisUser = "" Then Call UserDL Call ConnecttoDB Set cmd = New ADODB.Command: Set rs = New ADODB.Recordset With cmd .CommandTimeout = 120 .ActiveConnection = cn .CommandText = "CSLL.Reports" .CommandType = adCmdStoredProc .Parameters.refresh .Parameters("@Alias").value = ThisUser On Error GoTo NoConnection Set rs = .Execute On Error GoTo 0 End With With rs For i = 0 To .Fields.Count - 1 If i = 0 Then FBMap.Add .Fields.Item(0).Name, "Key" Else FBMap.Add .Fields.Item(i).Name, i - 1 End If Next i NoCol = .Fields.Count - 2 If Not .BOF And Not .EOF Then .MoveLast .MoveFirst While (Not .EOF) With FBList ReDim UStemp(0 To NoCol, 0) As Variant sKey = rs("ID") If Not .Exists(sKey) Then For i = 1 To NoCol + 1 UStemp(i - 1, 0) = rs(i) Next i .Add sKey, UStemp ElseIf .Exists(sKey) = True Then si = UBound(FBList(sKey), 2) ReDim UStemp(0 To NoCol, 0 To si + 1) For j = 0 To si + 1 If j <= si Then For i = 0 To NoCol UStemp(i, j) = .Item(sKey)(i, j) Next i ElseIf j > si Then For i = 0 To NoCol UStemp(i, j) = rs(i + 1) Next i End If Next j .Remove sKey .Add sKey, UStemp End If End With .MoveNext Wend .MoveFirst End If End With Set cmd = Nothing: Set rs = Nothing: Set cn = Nothing
-------- @Vityata Please look at this:
Option Explicit Public Sub ProofOfSlicingWithArray() Dim tmp(1 To 10, 1 To 10) As Variant Dim i As Long, j As Long ' Populate multi-dimensional array For i = 1 To 10 For j = 1 To 10 tmp(i, j) = Int((999 - 100 + 1) * Rnd + 100) Next j Next i Debug.Print Join(Application.Index(tmp, 5, 0), ",") End Sub
最佳答案
发生错误的原因是 Application.WorksheetFunction.Index
限制为 255 个字符,而数组的值有时会超出它。
正如我以前在代码中放入的 Application.Transpose(vArray)
一样,它是一个方便的解决方案,但 65536 行限制和 255 个字符限制使其不可靠,现在我只使用用于转置的自定义函数。
关于vba - 尝试对多维数组进行切片时类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40629434/