VBA – usage of user defined types

 

 

Probably you have heard something about PHP. If you have, then you know that it supports arrays (and lists and anything you like) and you may declare any type of data in these. In VBA, usually the case is a little different. If you have a list of integers, then you can have only integers in it, and if you have a list of doubles – only doubles.

Anyway, sometimes you may wish that VBA was a little bit like PHP, where you could have declared a list of various variables. Actually, this is possible 🙂

WIth a little work around. You should declare a Private Type, in which you may put strings, integers and anything. Let’s take a look at the example:

formula1Ranking

Imagine you have to organize a Formula1 Grand Prix. You declare the following private types:

Then you simply decide to assign values to these types and to let it run. In the following example I have “created” two “roads” with two cars per road. This is how it looks like:

This is the result in the immediate window:

ImmediateFormula

 

It is actually cool, if you can use it. 🙂 Or at least, you can use it to show, that you can put various types of data in a List or array. Which is a good usage as well 😀

About

VBA Developer

Tagged with: , ,