数组值未转换为单元格

时间:2015-10-10 15:26:02

标签: excel vba

我正在尝试学习如何在Excel VBA中使用字典。测试是通过一个数组从A列中的行1-100000获取所有值到一个字典,然后将所有值写入B列。这可以正常工作直到行34464,B列中的其余行只得到{{ 1}}。

任何想法为什么?

#N/A

1 个答案:

答案 0 :(得分:1)

由于工作表函数Transpose的限制(另请参阅Paul Bica发布的链接),您需要直接将元素分配给数组。以下应该有效:

Option Explicit
Sub nnn()

'Tools - References - Microsoft Scripting Runtime
Dim myArray As Variant
Dim myRow As Long
Dim dicMyDictionary As Scripting.Dictionary
Set dicMyDictionary = New Scripting.Dictionary

With ThisWorkbook.Worksheets("Sheet1")

    myArray = Range(Cells(1, 1), Cells(100000, 1)).Value

    For myRow = LBound(myArray, 1) To UBound(myArray, 1)
        dicMyDictionary.Add myRow, myArray(myRow, 1)
    Next myRow

    ReDim myArray(1 To dicMyDictionary.Count, 1 To 1)
    For myRow = 1 To UBound(myArray, 1)
        myArray(myRow, 1) = dicMyDictionary(myRow)
    Next myRow

    .Range("B1").Resize(dicMyDictionary.Count, 1).Value = myArray
    Set dicMyDictionary = Nothing

End With

End Sub