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
The Dictionary class is similar to the Collection class in that it allows you to store a custom collection of objects. This video teaches you how to use dictionaries, including how to reference the Scripting Runtime Object Library, how to create a new Dictionary and add, remove, count and reference the items in it. You'll also learn about the Items and Keys arrays contained within a Dictionary and how to loop over these arrays in order to process all of the items that the Dictionary contains.
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: | Certified |
When: | 07 Nov 18 at 15:07 |
I am getting an object error with this code.
Sub Schedule15(theDictionary As Dictionary)
Dim singleKey As Variant
For Each singleKey In theDictionary.Keys()
Debug.Print theDictionary.Item(singleKey).Country
Next singleKey
Do you know what can be causing it?
From: | duggie |
When: | 22 Sep 18 at 07:19 |
This code is early bound:
Dim DIC As Scripting.Dictionary
'Dim DIC As Object
Set DIC = New Scripting.Dictionary
'Set DIC = CreateObject("Scripting.Dictionary")
Dim MyArray() As Variant
MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
Dim n As Long
For n = 1 To UBound(MyArray(), 1)
DIC.Item(MyArray(n, 1)) = 0
Next n
Dim KeysArray() As Variant
KeysArray() = DIC.Keys
Dim NumKeys As Long
NumKeys = DIC.Count
Dim ElementsArray() As Variant
ReDim ElementsArray(1 To DIC.Count, 1 To 1) As Variant
Dim DataRng As Range
Set DataRng = Sheet1.Range(Sheet1.Cells(2, 1), Sheet1.Cells(Module1.LRow(wks:=Sheet1), 1))
Dim Counter As Long
For Counter = 1 To DIC.Count - 1
ElementsArray(Counter + 1, 1) = Application.WorksheetFunction.CountIf(DataRng, DIC.Keys(Counter))
Next Counter
To convert it to late bound, I have to chnage this line:
ElementsArray(Counter + 1, 1) = Application.WorksheetFunction.CountIf(DataRng, DIC.Keys(Counter))
to this:
ElementsArray(Counter + 1, 1) = Application.WorksheetFunction.CountIf(DataRng, DIC.Keys()(Counter))
What's the significance of the extra ()?
From: | Andrew G |
When: | 24 Sep 18 at 09:08 |
The Dictionary.Keys() method is a function with no parameters which returns an array. A very basic definition for this function might look like this:
Public Function Keys() As Variant()
Keys = Array(1, 2, 3)
End Function
In order to return a single value from the array returned by the function you might assume that you can write this:
Sub TestKeys()
Debug.Print Keys(0)
End Sub
This doesn't work as the compiler thinks you're attempting to pass an argument to a parameter of the function.
The correct syntax to return a value from the array returned by the function is:
Sub TestKeys()
Debug.Print Keys()(0)
End Sub
The error message you see when you try to use the first syntax with a late-bound Dictionary is:
"Property let procedure not defined and property get procedure did not return an object"
My guess is that the compiler assumes that you're attempting to pass a value to a parameter of a Property Let or Property Get procedure. With early-binding it knows that there is a Keys function which has no parameters and calls it accordingly, treating the value in parentheses as the index of the array element you're requesting.
My advice would be to always use the full syntax Keys()(element_index) regardless of whether you're using early or late binding.
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.