You can use your favorite social network to register or link an existing account:
Or use your email address to register without a social network:
Sign in with these social networks:
Or enter your username and password
Forgot your password?
Yes, please link my existing account with for quick, secure access.
No, I would like to create a new account with my profile information.
Today's author, Stephane Viot, a Program Manager in the Office Global Experience Platform team, reflects on how leveraging application features could help speed up the development of your VBA solutions, while improving their performance.
Often -but hopefully not always- VBA developers forget about the "A" in "VBA". And by that I mean they might have the tendency of trying to do everything in VBA, like they would in VB, forgetting about what the host application has to offer. To illustrate that point -that is the main purpose of this post- let me share with you a practical example.
I often need to extract the list of unique values from a range -usually from a list or a table- and paste it into a mail as a comma-delimited string where all values are sorted in alphabetical order.
For example, given the following list, how would you pull out the list of unique states you are shipping products to, in alphabetical order, using VBA code -so that you can re-use it over and over?
Before actually getting into the details of a better solution, let's see how one could have tackled the problem, using VBA code only. Even if variants exist, the basic steps would be to:
1. Loop through each cell in the current selection and construct an array of unique values.
2. Sort the array.
3. Build the output string using the list separator provided by the user.
4. Copy the output string to the Clipboard.
When working on very large lists, extracting the unique values from a range using the preceding technique might show its limits, and be quite slow. Sorting the values, too.
Now, think how the application could help you do such things. In other words, what steps would you go through if you had to do it "manually"?
This is what I would personally do.
On very large lists, I can guarantee that you will be amazed to see how fast Excel Remove Duplicates can be. And I would bet, without taking any risks, that it would be much faster than any VBA code anyone could write.
As you have probably guessed it by now, a better solution to the Vba-only approach would be to mimic those manual steps.
Rather than providing the full VBA code listing here (available for download at the end of this post), let's instead see how the add-in is organized.
The CopyUniqueValues add-in contains:
Below are the delimiter and sorting options users can select:
Note: The choice of the delimiter would be determined by the intended use. For example, for building a list...
The GetUniqueStringsList function, in modCopyUniqueValues, is responsible for creating the sorted list of unique values. It relies heavily on Excel features exposed through its Object Model. It performs the following actions:
The intent of this article was to remind (as a piece of advice) VBA developers to always check how the host application could help them solve complex operations, before exercising their coding skills.
When writing Excel macros, I am indeed relying on the application even for the simplest tasks (such as the one below), saving me literally lines of VBA code. For example, I would often favor retrieving a SQL statement I want to use for querying a database from a cell rather than construct it programmatically:
That way, I would simply get the value of the SQL statement from cell A3 and be ready with it.
Comments: (5) Collapse
This is a very good thing to remind programmers of. Unfortunately, many programmers are as unfamiliar with the parent application as they are with the business domain they are programming for.
Excel is a fine platform for application development, because it has an extensive calculation engine and a decent presentation layer. But I have seen Excel VBA "solutions" (I hesitate to use that word) which reproduce many built-in features of Excel, usually much less efficiently.
Very true. The "dive for VBA" thing is symptomatic of the user who's trying to make a little knowledge go too far.
Throw in a good understanding of indirection, offset, lookups, match/index and array formulae, and there's an astonishing amount one can achieve without ever getting to the VBE. And once that's mastered, I reckon the user will know when to go to code.
I have to confess to having written a VBA "RangeJoin" function, that wraps Join(), mind you.
Stephane, VBA is faster than you may think. I can read in and sort unique values from 60,000 cells, in just 0.2 seconds.
This is because VBA can create unique lists extremely quickly using Collections, and readily available QuickSort code handles the sorting.
So VBA can replicate the functionality of the add-in without using any Excel functions. That doesn't mean it's a better solution, naturally, but let's not undersell VBA!
Thank you all for your comments.
Dermot, I guess my post is as much about performance as it is about not "reinventing the wheel". Your observation as well as Dany's excellent article ("What is the fastest way to scan a large range in Excel?" at blogs.msdn.com/.../what-is-the-fastest-way-to-scan-a-large-range-in-excel.aspx) clearly show that VBA can be extremely fast... when used properly. The other benefits of taking advantage of the application OM usually are more compact code, less complex code, and (in my case :-)) less buggy code. Think about the number of developers and testers behind the OM...
please don't forget about the speed in excel 2003 and port it over to excel 2007 in sp2. excel code execution in excel 2007 is atrocious and you should be ashamed of yourselves.
Comments: (loading) Collapse