Add item to collection with key or before/after

paipimenta

Board Regular
Joined
Apr 7, 2010
Messages
103
I'm trying to add values in a range to a collection. here's the range:
<TABLE style="WIDTH: 103pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=137 x:str><COLGROUP><COL style="WIDTH: 103pt; mso-width-source: userset; mso-width-alt: 5010" width=137><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 103pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17 width=137>.............A
1 Product
</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>2 Pricing</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>3 Environment</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>4 Customer Service</TD></TR></TBODY></TABLE>

Here's my code:
Code:
Sub loadCollection()
    Dim mCats As New Collection
    Dim cl, part As Variant
    For Each cl In Range("A1:A4")
        mCats.Add(cl.Value)
    Next cl
    For Each part In mCats
        MsgBox part
    Next part
End Sub
This works fine, however, when I change the mCats.Add line to the following:
Code:
mCats.Add(cl.Value,,1)
in order to insert them in reverse order, it says "Expected: =" same thing if I put in a key or an after value. Compile error, not run-time. What's up?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This works, in order to add before or after an item in the collection, that item needs to exist.

Code:
Sub loadCollection()
    Dim mCats As New Collection
    Dim cl, part As Variant
    mCats.Add Item:="x", Key:="x"
    For Each cl In Range("A1:A4")
        mCats.Add Item:=cl.Value, Key:=cl.Value, after:=1
    Next cl
    mCats.Remove 1
    For part = 1 To mCats.Count
        MsgBox mCats(part)
    Next part
End Sub
 
Upvote 0
How about replacing For Each with For.
Code:
Sub loadCollection()
    Dim mCats As New Collection
    Dim cl, part As Variant
    Dim i as Long
    'For Each cl In Range("A1:A4")
    For i = Range("A1:A4").Cells.Count To 1 Step -1
        Set cl = Range("A1:A4").Cells(i)
        mCats.Add (cl.Value)
    Next
    For Each part In mCats
        MsgBox part
    Next part
End Sub
 
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