Excel - VBA & Macros 2010 - Defined Constants: - Episode 1222

745 views · Published 18 August 2010 · 2:44 · Indexed 3 October 2026

Channel: MrExcel.com · 2010 · Education

Watch on YouTube

Microsoft Excel Tutorial: An explanation of how defined constants such as XLRight make life easier in VBA. 

Welcome to another episode of the MrExcel podcast, sponsored by MrExcel.com. In this video, we will be diving into the world of Excel VBA and discussing one of its mysteries - defined constants. These constants are there to make your life easier and we will show you how to use them effectively in your VBA code.

To begin, we will turn on the macro recorder and record a new macro. We will then add some data and do some formatting, including aligning text and adding vertical lines. Once we stop the recording and take a look at the code, we will see some unfamiliar terms such as xlRight and xlBottom. These are known as defined constants and they are there to simplify your code.

Imagine having to write code where you have to remember and type in a long number like -4152 for horizontal alignment. It would be a nightmare! That's where defined constants come in. Instead of using the number, we can simply use the constant xlRight. This makes our code much more readable and easier to remember. And for those of you in English-speaking countries, you're in luck because these constants are permanently stored and can be accessed by simply typing in their name.

But how do we know if we are using the correct constant? Well, a handy tip is to type it in lowercase and if it gets automatically capitalized, then you know you have it right. If not, then it's not a defined constant. Just imagine how much more difficult VBA would be without these constants. We would have to remember long numbers for every single formatting option. Thanks to defined constants, our code is much more efficient and easier to write.

So there you have it, defined constants are a game-changer in the world of Excel VBA. They make our code more readable, efficient, and easier to remember. We hope you found this video helpful and stay tuned for more tips and tricks from MrExcel. Don't forget to like, comment, and subscribe to our channel for more Excel goodness. See you next time!

Buy Bill Jelen's latest Excel book: https://www.mrexcel.com/products/latest/

You can help my channel by clicking Like or commenting below: https://www.mrexcel.com/like-mrexcel-on-youtube/


Table of Contents:
(00:00) Excel VBA Defining Constants
(00:16) Recording a New Macro
(00:26) Adding Content and Formatting
(00:43) Examining the Code
(00:54) Understanding Defined Constants
(01:04) Simplifying Code with Defined Constants
(01:14) Benefits of Defined Constants
(01:28) Explanation of xlRight and xlBottom
(01:43) Advantages for English-Speaking Countries
(02:04) Remembering Defined Constants
(02:14) Identifying Correctly Typed Defined Constants
(02:24) Making VBA Easier with Defined Constants
(02:34) Clicking Like really helps the algorithm

#excel #microsoft #microsoftexcel #exceltutorial #exceltips #exceltricks #excelmvp #freeclass #freecourse #freeclasses #excelclasses #microsoftmvp #walkthrough #evergreen #spreadsheetskills #analytics #analysis #dataanalysis #dataanalytics #mrexcel #spreadsheets #spreadsheet #excelhelp #accounting #tutorial 

This video answers these common search terms:
Benefits of defined constants in VBA
Benefits of using defined constants for alignment
Difference between xlRight and xlBottom
Excel VBA chapter 2
Excel VBA macro recorder
Formatting in Excel VBA
How to identify correct defined constants
Importance of defined constants in VBA
Making VBA coding easier with defined constants
MrExcel podcast on Excel VBA
Using defined constants in VBA code
xlRight and xlBottom constants

Join the MrExcel Message Board discussion about this video at https://www.mrexcel.com/board/threads/1152904/

More from this channel