Excel VBA Scripting Dictionary Output Issue

mekueny

New Member
Joined
Feb 26, 2019
Messages
2
In VBA I have created a scripting dictionary called finDict that contains ~72k keys and items. I'm trying to write the list of keys to a worksheet.

The code below correctly prints all of the keys in finDict, but it is extremely slow:

For Z = 1 To finDict.Count
ActiveWorkbook.Sheets("Test").Range("A" & Z) = finDict.Keys(Z - 1)
Next Z

I tried replacing that code with the code below, but for some reason this code replaces most of the printed keys with "#N/A":

ActiveWorkbook.Sheets("Test").Cells(1, 1).Resize(finDict.Count) = Application.Transpose(finDict.Keys)

Can anyone spot what the issue is??
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello mekueny,

Transpose has a cutoff off 8192 distinct range areas. It is much faster to load the values into a 2-D array then output the array to the range. Here is an example...
Code:
Sub Macro2()


    Dim Dict    As Object
    Dim k       As Long
    Dim Key     As Variant
    Dim Keys    As Variant
    
        Set finDict = CreateObject("Scripting.Dictionary")
        
        ' // Code to load the dictionary goes here
        
        ReDim Keys(1 To Dict.Count, 1 To 1)
        
        For Each Key In Dict.Keys
            k = k + 1
            Keys(k, 1) = Key
        Next Key
        
        ActiveWorkbook.Sheets("Test").Range("A1").Resize(finDict.Count, 1).Value = Keys
        
End Sub
 
Upvote 0
Transpose is limited to 65,536 elements, see this discussion:
https://www.mrexcel.com/forum/excel-questions/555073-limitation-range-size-transpose-function.html

A work around:

Code:
[FONT=lucida console][COLOR=Royalblue]Dim[/COLOR] x, va
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]

[COLOR=Royalblue]ReDim[/COLOR] va([COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] finDict.count, [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]1[/COLOR])
    [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] x [COLOR=Royalblue]In[/COLOR] finDict.Keys
        i = i + [COLOR=crimson]1[/COLOR]
        va(i, [COLOR=crimson]1[/COLOR]) = x
    [COLOR=Royalblue]Next[/COLOR]
ActiveWorkbook.Sheets([COLOR=brown]"Test"[/COLOR]).Cells([COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR]).Resize(finDict.count) = va[/FONT]

Edited: Ah, Leith Ross already got the answer.:)
 
Last edited:
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top