尝试切片多维数组时键入不匹配

时间:2016-11-16 10:15:02

标签: vba excel-vba excel

我有一个存储在字典中的数组,它的边界来自(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), ",")

我总是在最后一行遇到类型不匹配。我已经成功地将其与其他阵列一起使用但不是这次。

Example File

  

更新:填充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请看这个:

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

1 个答案:

答案 0 :(得分:0)

发生错误是因为Application.WorksheetFunction.Index限制为255个字符,而且数组的值有时会超过它。

正如我曾经在我的代码中放置Application.Transpose(vArray)一样,这是一个方便的解决方案,但65536行限制和255个字符限制使它不可靠,现在我只使用自定义函数进行转置。