Write Collection as Range to Worksheet

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
105
Office Version
  1. 365
Platform
  1. Windows
How do you write the contents of a collection as a range instead of outputting cell by cell? I would like to write all 50 values with one command,
unlike below. Thanks.

Code:
    Dim mycol As Collection: Set mycol = New Collection
        
    ' fill the collection
    For x = 1 To 50
        mycol.Add (x)
    Next x

    ' output to sheet one at a time
    For x = 1 To 50
        Sheets("sheet1").Cells(x, 4) = mycol(x)
    Next x
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
As far as I know, Collections.Items can be accessed sequentially ONLY and written to sheet. NOT enbloc. Better to use an array if you want to access sheet less often.

If you must use collection, then you can first write the Collection to an array in a loop and then write the array to sheet enbloc. Depending on the number of elements, this may still be faster. Though not as fast as using an array in the first place!

Incidentally, it is better to use Dictionary objects rather than Collections. Check this: http://www.cpearson.com/Excel/CollectionsAndDictionaries.htm This site also gives ways to translate between Collections/Dictionary/Arrays/Ranges.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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