Collection Code error

Juan Cornetto

New Member
Joined
Mar 5, 2013
Messages
44
I am using the following code to create a unique sorted collection for populating a combobox in a user form but get a "Subscript out of range" error.

Can anyone spot the error please ?

Private Sub Worksheet_Activate()

Dim colMyCol As New Collection
Dim rRange As Range
Dim rCell As Range
Dim lCount As Long
Dim sVar As String

On Error GoTo ErrorHandle

Set rRange = Range("C1")

If Len(rRange.Value) = 0 Then GoTo BeforeExit

If Len(rRange.Offset(1, 0).Value) > 0 Then
Set rRange = Range(rRange, rRange.End(xlDown))
End If

On Error Resume Next

For Each rCell In rRange

If IsNumeric(rCell.Value) Then
sVar = Str$(rCell.Value)
Else
sVar = rCell.Value
End If

With colMyCol

If .Count > 0 Then
For lCount = 1 To .Count
If rCell.Value < .Item(lCount) Then
.Add rCell.Value, sVar, lCount
Exit For

End If
Next
End If

If lCount = .Count + 1 Or .Count = 0 Then
.Add rCell.Value, sVar
End If
End With
Next

On Error GoTo ErrorHandle

Set rRange = Range("P1")

With colMyCol
For lCount = 0 To .Count
rRange.Offset(lCount, 0).Value = .Item(lCount + 1)
Next
End With

BeforeExit:
Set colMyCol = Nothing
Set rRange = Nothing
Set rCell = Nothing

Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,

When I tried it the fault was here:

Code:
        For lCount = 0 To .Count
            rRange.Offset(lCount, 0).Value = .Item(lCount + 1)
        Next

There were 8 things in my list, items 1 to 8.
The loop runs from 0 to 8.
So lCount+1 runs from 1 to 9 but 9 is bigger than 8 so subscript out of range.

Try:
Code:
        For lCount = 0 To .Count - 1
            rRange.Offset(lCount, 0).Value = .Item(lCount + 1)
        Next
 
Upvote 0

Forum statistics

Threads
1,203,096
Messages
6,053,515
Members
444,669
Latest member
Renarian

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