VBA & Macros 2010 - Defined Constants: #1222

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 Aug 18, 2010.
An explanation of how defined constants such as XLRight make life easier in VBA. Episode #1222 shows an example of the power and versatility of VBA in Microsoft Excel. ...This is the podcast companion to the book, "Excel 2010 In Depth", by Bill Jelen
maxresdefault.jpg


Transcript of the video:
MrExcel podcast is sponsored by Easy-XL.
Excel VBA, chapter 2.
Define Constants.
All right! 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 in some stuff and some other stuff.
And then let's do some formatting here.
I'll take that first word and align right and the second word will 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 working here, our project explorer, find that new model -1 that was inserted.
Alright! Here we go.
Now, let's look at this xlRight.
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 gonna 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 equal to (=) negative (-) 4152.
Alright!
Think about how horrible that code would be.
So, whoever came up with VBA decided that negative -4152 is what xlRight is the...
When you write a line, you're going to use negative 4152 xlBottom.
Let's take a look at that...
that's negative -4107, who would ever remember this stuff.
Okay! 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 negative 4152 and 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 a miss-spelling.
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 VBA, a lot easier.
Just imagine, how bad it would be if we had a great code like, vertical equals negative 4107, instead of xlBottom.
Well, there you have it.
Thank you for stopping by.
We’ll see you next time for another netcast, from MrExcel.
 

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

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