Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
544 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Posted by Andrew Gould on 27 October 2014
Excel VBA is full of collections and you can even create your own custom collections using the Collection object. This video teaches you how to create a new custom collection, how to add items to it, how to reference those items individually and how to loop over all of the items in the collection. You'll also see how you can add items based on a custom class module to a collection.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
There are no files which go with this video.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | duggie |
When: | 29 Jan 22 at 14:14 |
I came across this code from another website:
These lines are in the class collectionExt:
Public Function Clone() As collectionExt
Dim newColl As New collectionExt
' some more code
End Function
It seems inside the function, it is decalring an instance of itself. How come it's not recursive?
From: | Andrew G |
When: | 31 Jan 22 at 15:46 |
Hi duggie!
It would be recursive if the Clone function called itself. Or if the Initialize event of the class called the Clone function. Or if the Initialize event of the class created a new instance of itself.
As it is, the Clone function creates one new instance of the class each time the Clone function is called.
I hope it helps!
From: | duggie |
When: | 20 Sep 18 at 21:44 |
This code removes all rows if column E contains Oranges
Dim DataArray() As Variant
DataArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
Dim DataArrayRows As Long
DataArrayRows = UBound(DataArray(), 1)
Dim DataArrayCols As Long
DataArrayCols = UBound(DataArray(), 2)
Dim SubArray() As Variant
ReDim SubArray(1 To DataArrayRows, 1 To DataArrayCols) As Variant
Dim I As Long, j As Long, k As Long
j = 1
k = 1
For i = 2 To DataArrayRows
If DataArray(i, 5) <> "Oranges" Then
For j = 1 To DataArrayCols
SubArray(k, j) = DataArray(i, j)
Next j
k = k + 1
End If
Next I
Sheet3.Cells(2, 1).Resize(k - 1, DataArrayCols).Value = SubArray()
but I've had to declare a large SubArray, as big as the original array.
ReDim SubArray(1 To DataArrayRows, 1 To DataArrayCols) As Variant
Is it possible to use collections to "add-on-the-fly"?
Dim DataArray() As Variant
DataArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
Dim DataArrayRows As Long
DataArrayRows = UBound(DataArray(), 1)
Dim DataArrayCols As Long
DataArrayCols = UBound(DataArray(), 2)
Dim SubArray() As Variant
ReDim SubArray(1 To DataArrayRows, 1 To DataArrayCols) As Variant
Dim I As Long, j As Long, k As Long
j = 1
k = 1
Dim Coll1 As Collection
Dim Coll2 As Collection
Dim Coll3 As Collection
Dim Coll4 As Collection
Dim Coll5 As Collection
Dim Coll6 As Collection
Dim Coll7 As Collection
Dim Coll8 As Collection
Dim Coll9 As Collection
Dim Coll10 As Collection
Set Coll1 = New Collection
Set Coll2 = New Collection
Set Coll3 = New Collection
Set Coll4 = New Collection
Set Coll5 = New Collection
Set Coll6 = New Collection
Set Coll7 = New Collection
Set Coll8 = New Collection
Set Coll9 = New Collection
Set Coll10 = New Collection
For i = 2 To DataArrayCols
If DataArray(i, 12) <> "RD" Then
Coll1.Add DataArray(i, 1)
Coll2.Add DataArray(i, 2)
Coll3.Add DataArray(i, 3)
Coll4.Add DataArray(i, 4)
Coll5.Add DataArray(i, 5)
Coll6.Add DataArray(i, 6)
Coll7.Add DataArray(i, 7)
Coll8.Add DataArray(i, 8)
Coll9.Add DataArray(i, 9)
Coll10.Add DataArray(i, 10)
k = k + 1
End If
Next i
Sheet3.Cells(2, 1).Resize(k - 1, DataArrayCols).Value = SubArray()
but it seems cumbersome.
How can I use a collection without having to declare it many times?
From: | Andrew G |
When: | 21 Sep 18 at 09:12 |
I don't think that a collection will help you here. Regardless of how you populate it, you won't be able to write its contents into a range of cells in a single step in the same way you can with an array. You would have to loop over the collection after you have populated it and write out its contents one-by-one.
For the sake of interest, you could use a single collection as shown below:
Sub PopulateCollection()
Dim DataArray
Dim DataArrayRows As Long
Dim DataArrayCols As Long
Dim i As Long, j As Long
Dim Coll1 As Collection
DataArray = Sheet1.Cells(1, 1).CurrentRegion
DataArrayRows = UBound(DataArray, 1)
DataArrayCols = UBound(DataArray, 2)
Set Coll1 = New Collection
'Loop through array and add matching values to collection
For i = 2 To DataArrayRows
If DataArray(i, 12) <> "RD" Then
For j = 1 To DataArrayCols
Coll1.Add Item:=DataArray(i, j)
Next j
End If
Next i
'Loop through collection and list its values
Dim v As Variant
For Each v In Coll1
Debug.Print v
Next v
End Sub
You may also be interested in this post which talks about ways to create a key for each item in the collection https://stackoverflow.com/questions/5702362/vba-collection-list-of-keys
I hope that helps!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.