Excel VBA 2 - Defined Constants

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Jul 9, 2010.
An explanation of how defined constants such as XLRight make life easier in VBA
maxresdefault.jpg


Transcript of the video:
MrExcel podcast is sponsored by “Easy-XL”!
Excel VBA chapter 2 - Defined Constants!
Alright so, let's talk about one of the mysteries of Excel VBA.
I'm just going to turn on the macro recorder here, go to Developer, will record a new macro.
I'll just leave it like this, we’ll put on some stuff, some other stuff, and then let's do some formatting here.
Take that first word and align right, and the second word we’ll align center, and maybe do some vertical line top with that one, alright, good enough, Stop!
Let's go take a look at the code, Alt+F11, and we come over here to Project Explorer, find that new Module1 that was inserted, alright, here we go.
Now let's look at this, xlRight, xlBottom, what the heck is this stuff?
These are known as defined constants, and believe it or not, they're there to make your life easier.
In reality what should be happening, I’m going to come down here and say Print xlRight.
What should be happening is that you would have to write code, where you said “Hey, the horizontal alignment is =-4152”.
Alright, think about how horrible that code would be.
So whoever came up with VBA decided that -4152 is what xlRight is- when you write a line, you're going to use -4152.
xlBottom, let's take a look at that, that's -4107, who would ever remember this stuff?
OK, so if you happen to be in an English-speaking country, you're going to love this, because what they said was, “Well, rather than put in 4152, we're going to permanently store.” In other words, it's a defined constant, the value of -4152 in a variable called xlRight.
So beautiful thing is, you don't have to remember 4152, because who would ever remember that?
You just have to remember xlRight.
Notice I always type it lowercase, and then, when I go to a new line, I should see at least one character get capitalized, that means that I typed it correctly.
If nothing is capitalized, let's do xlwrite, which obviously is misspelling, that's NOT a defined constant, so that's how you can tell whether you've got it or not.
It's kind of an interesting way to actually make me VBA a lot easier.
Just imagine how bad it would be, if we had to write code like vertical alignment =-4107 instead of xlBottom.
Well, there you have it, thanks for stopping by, we’ll see you next time for another netcast from MrExcel!
 

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top