tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,926
- Office Version
- 365
- 2019
- Platform
- Windows
Further to this thread I started:
Here's the code:
ClsData:
Standard Module:
This is the new line I added:
and it hangs around that line for a very long time. (There are 300,000 values in column A).
Is that expected or should clearing collections be fast?
If it's expected, should I not bother clearing collections, classes, arrays after using them because they'd clear automatically once outside the Sub?
Thanks
Rich (BB code):
https://www.mrexcel.com/forum/excel-questions/1052067-collection-slow.html
Here's the code:
ClsData:
Rich (BB code):
Option Explicit
Private pDayOfWeek As String
Private pFruit As String
Public Property Get DayOfWeek() As String
DayOfWeek = pDayOfWeek
End Property
Public Property Let DayOfWeek(ByVal D As String)
pDayOfWeek = D
End Property
Public Property Get Fruit() As String
Fruit = pFruit
End Property
Public Property Let Fruit(ByVal F As String)
pFruit = F
End Property
Standard Module:
Rich (BB code):
Dim DataArray() As Variant
DataArray = Sheet1.Cells(1, 1).CurrentRegion.Value
Dim DataArrayRows As Long
DataArrayRows = UBound(DataArray(), 1)
Dim FruitArray() As Variant
ReDim FruitArray(1 To DataArrayRows, 1 To 1) As Variant
Dim Counter As Long
Dim MyData As ClsData
Dim MyColl As Collection
Set MyColl = New Collection
For Counter = 2 To DataArrayRows
Set MyData = New ClsData
MyData.DayOfWeek = DataArray(Counter, 1)
Select Case MyData.DayOfWeek
Case "Monday"
MyData.Fruit = "Orange"
Case "Tuesday"
MyData.Fruit = "Apple"
Case Else
MyData.Fruit = "Banana"
End Select
MyColl.Add MyData
Next Counter
Dim c As ClsData
Dim i As Long
For Each MyData In MyColl
i = i + 1
FruitArray(i, 1) = MyData.Fruit
Next MyData
Set MyColl = Nothing 'NEW LINE
Sheet1.Cells(2, 2).Resize(DataArrayRows, 1).Value = FruitArray()
This is the new line I added:
Rich (BB code):
Set MyColl = Nothing
and it hangs around that line for a very long time. (There are 300,000 values in column A).
Is that expected or should clearing collections be fast?
If it's expected, should I not bother clearing collections, classes, arrays after using them because they'd clear automatically once outside the Sub?
Thanks
Last edited: