Collection Class - How to remove specific object

Fizzyfish

New Member
Joined
Aug 3, 2011
Messages
13
I've figured out how to add items to a class collection and how to update them, but I need to remove an item from a class collection. I can't seem to get a handle on this and would appreciate any help with it. Below is a sample of the code I have been working on.

Thanks,
Sherry

Dim g_colSummaryRows As New clsSummaryRows

************************************************

Class Module: clsSummaryRows
Public RowName as String
Public ActualQ1Revnue as double

************************************************

Class Module: clsSummaryRows

Public Sub Add(recSummaryRow As clsSummaryRow)
AllSummaryRows.Add recSummaryRow
End Sub

Public Property Get Count() As Long
Count = AllSummaryRows.Count
End Property

Public Property Get items() As Collection
Set items = AllSummaryRows
End Property

Public Property Get item(MyItem As Variant) As clsIncomeStatementRow
Set item = AllSummaryRows(MyItem)
End Property

Public Sub Remove(MyItem As Variant)
AllSummaryRows.Remove (MyItem)
End Sub

**********************************************


Sub RemoveRowfoundInCollection(recIncomeStatementRows as ADODB.Recordset)

if rowname from database matches rowname in collection, I want to remove the item from the collection

With recIncomeStatementRows
For RowCounter = 1 to .RecordCount
'Check if row is in collection
if g_SummaryRows.count> 0 then
For RecordCounter = 1 to g_SummaryRows.Count
If !RowName = g_SummaryRows.item(RecordCounter).RowName then
'Row name was found in collection, so remove item from collection
**How do I remove this item from the collection g_SummaryRows please?**
end if​
Next RecordCounter 'go to next g_SummaryRows item​
end if​
.MoveNext 'go to next IncomeStatementRows record
next RowCounter​
end with

End sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I wanted to remove an item from an object and then add it back in with a new value, but I figured out how to update the value of the item in the collection instead. Below is the procedure I wrote to accomplish this:

Sub UpdateSummaryCollectionRowValue(strRowName As String, ActualQ1 As Double, ActualQ2 As Double, _
ActualQ3 As Double, ActualQ4 As Double, ForecastQ1 As Double, ForecastQ2 As Double, ForecastQ3 As Double, ForecastQ4 As Double, _
BudgetQ1 As Double, BudgetQ2 As Double, BudgetQ3 As Double, BudgetQ4 As Double)
On Error GoTo ErrorHandler:

Dim RowCounter As Integer

'Find RowName is in the collection
If g_colSummaryRows.Count > 0 Then
For RowCounter = 1 To g_colSummaryRows.Count
'If strRowName = g_colSummaryRows.item(RowCounter).RowName Then
If g_colSummaryRows.items(RowCounter).RowName = strRowName Then
'RowName found in collection, so Update the values
g_colSummaryRows.items(RowCounter).ActualQ1Revenue = g_colSummaryRows.items(RowCounter).ActualQ1Revenue + ActualQ1
g_colSummaryRows.items(RowCounter).ActualQ2Revenue = g_colSummaryRows.items(RowCounter).ActualQ2Revenue + ActualQ2
g_colSummaryRows.items(RowCounter).ActualQ3Revenue = g_colSummaryRows.items(RowCounter).ActualQ3Revenue + ActualQ3
g_colSummaryRows.items(RowCounter).ActualQ4Revenue = g_colSummaryRows.items(RowCounter).ActualQ4Revenue + ActualQ4
g_colSummaryRows.items(RowCounter).ForeCastQ1Revenue = g_colSummaryRows.items(RowCounter).ForeCastQ1Revenue + ForecastQ1
g_colSummaryRows.items(RowCounter).ForeCastQ2Revenue = g_colSummaryRows.items(RowCounter).ForeCastQ2Revenue + ForecastQ2
g_colSummaryRows.items(RowCounter).ForeCastQ3Revenue = g_colSummaryRows.items(RowCounter).ForeCastQ3Revenue + ForecastQ3
g_colSummaryRows.items(RowCounter).ForeCastQ4Revenue = g_colSummaryRows.items(RowCounter).ForeCastQ4Revenue + ForecastQ4
g_colSummaryRows.items(RowCounter).BudgetQ1Revenue = g_colSummaryRows.items(RowCounter).BudgetQ1Revenue + BudgetQ1
g_colSummaryRows.items(RowCounter).BudgetQ2Revenue = g_colSummaryRows.items(RowCounter).BudgetQ2Revenue + BudgetQ2
g_colSummaryRows.items(RowCounter).BudgetQ3Revenue = g_colSummaryRows.items(RowCounter).BudgetQ3Revenue + BudgetQ3
g_colSummaryRows.items(RowCounter).BudgetQ4Revenue = g_colSummaryRows.items(RowCounter).BudgetQ4Revenue + BudgetQ4
Exit For
End If
Next RowCounter​
End If

Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox ("Error:" & Err.Number & " " & Err.Description)​
End Select​
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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