2016-11-16 79 views
4

我有一个存储在字典中的数组,其范围是从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 
+0

显示你如何填写'FBList' – user3598756

+0

只是好奇 - 你有索引与数组?我认为它只适用于范围 - https://support.office.com/en-us/article/INDEX-function-a5dcf0dd-996d-40a4-a822-b56b061328bd – Vityata

+1

@Vityata [看看这个](https: //usefulgyaan.wordpress.com/2013/06/12/vba-trick-of-the-week-slicing-an-array-without-loop-application-index/) – Tom

回答

0

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

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