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
538 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 ...
How to loop over enumerations in VBA |
---|
A short blog explaining how to use Visual Basic for Applications to loop over enumerations. |
I've blogged previously about enumerations as part of our Excel VBA tutorial, but never knew you could loop over them!
This blog was inspired by someone describing Andrew - a fellow blogger at Wise Owl - as "brilliant, fantastic, amazing, and all enumerations on those terms!".
Here's how to do this. First create your enumeration:
Public Enum AndrewQuality
Brilliant
Fantastic
Amazing
End Enum
Now loop over it from beginning to end:
Sub DescribeAndrew()
'variable to represent each quality
Dim q As AndrewQuality
'variable to hold final message
Dim msg As String
'loop over qualities
msg = "Qualities of Andrew are:" & vbCrLf
For q = Brilliant To Amazing
msg = msg & vbCrLf & q
Next q
'display the final qualities list
MsgBox msg
End Sub
When you run the DescribeAndrew procedure above, you'll get a message displaying his alleged qualities:
The message box lists out the numerical values of Andrew's qualities.
Unfortunately there's no way in VBA of showing the text equivalent for these numerical values, apart from writing your own SELECT CASE statement to return the right text value for each number.
For even more tricks with enumerations, go to (who else?) Chip Pearson.
Some other pages relevant to the above blog include:
From: | nimrod1313 |
When: | 11 Apr 19 at 18:22 |
Your sample code IS NOT iterating through the values of the enum. All the code does is iterate through all the numbers that exist between Brilliant ( i.e.0) and Amazing (i.e. 2).
To prove this assign values to the enums like brilliant = 1 , fantastic = 5 , Amazing = 10. Now when you run the code you should get a return of 1,5,10 BUT YOU DON'T you get a return of 1,2,3,4,5,6,7,8,9,10. So it's not iterating through the 3 enum values it's just spitting out numbers. between 1 and 10.
From: | Andy B |
When: | 12 Apr 19 at 10:21 |
I must admit that's what I had meant, but thanks for clarifying this as I obviously hadn't made myself clear. I must admit in 30+ years of programming I've never had call to do this, but it's nice to know it's possible!
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.