Working with Variables



Working with VariablesSession Number: MMW106-1Session Length: 0:11:23It is not unusual for your macros to perform some sort of calculation. It is a normal part of what they do. These calculations are made using variables, which are the building blocks of the calculating portion of your macros. Just like different parts of a building may require different sizes of bricks, different portions of your macros require different types of variables.Variables are nothing but a storage space for values. If you prefer a real-world analogy, you can think of them as containers that hold information. They are called variables because their contents can vary, meaning they can be changed. Just as you can change what is stored inside a container like a drinking glass, you can change what is stored within a variable.Variables come in different types. These are often called data types, and you can properly handle different types of information using these data types. Because VBA provides different types of variables that you can use in your programs, you must learn about them so that you can use them effectively.There are 11 data types supported by VBA. I have put together a quick guide to those data types, and you will find it in the document that accompanies this session. There is actually a 12th data type known as Decimal that is specified in the VBA documentation, but it is not currently supported by the language. And quite honestly, it is doubtful it ever will be supported since VBA has been around for two decades now without any change in the specification or any change in the lack of support for that data type.You probably already figured out that there is no companion Word document for this particular session in the course, so I have open on the screen a blank document that I just created a moment ago. I want to go ahead and press Alt+F11 to display the Visual Basic Editor. You will note that there is nothing in a Code window here. In fact, the Code window is missing completely and that is because we have just created a brand-new document, so there are no modules for our programming code within this particular document.I am going to go ahead and, using techniques that you learned earlier in this course, I am going to right-click on the project for this document and I am going to say Insert | Module. Now all of a sudden, we have a module here that we can start to do some work with. I will enlarge this just a little bit so we can work with it. The very first task that I am going to do is I am going to create a subroutine in here. It does not really matter what I call this. I am just going to call it MyMacro. When I hit Enter you will see that we have the framework for a subroutine procedure within this particular Code window.I am going to hit Enter a few times, so that we have a little bit of space in here, and I want to show you a few things about working with variables. In order to make sure that your variables match the type of data that you plan on storing in it, you should explicitly declare your variables near the beginning of your procedure. So right up here near the beginning of this particular subroutine is where I would personally choose to declare my variables.You do the declaration by using the Dim keyword. In other words, let us say that I had a variable that I wanted to declare as an Integer data type. I would use the keyword Dim, like I said just a second ago. Then I would put the name of the particular variable that I want to declare, and I am going to call this one iCounter. Then I am going to use the keyword As and then indicate what type of data type I want this particular variable to be considered. In this case, I am going to say that it is going to be an Integer.When I hit Enter, you can see that I have a fully executed line right here that tells me that my iCounter variable has been declared as an Integer. That is what defines how much space it takes up in the program and what I can store within that variable. Now, there is nothing really special about the "i" here in front of the word Counter. I like to put a lowercase character in the beginning of that to indicate what type of variable it is, and since this is an Integer, I put a lowercase "i" in front of it. Now, that is just my personal preference. You do not have to do that within your own macros, but I find it handy when I am creating my own variables and declaring them within my macros.If you take some time to look at the 11 data types detailed in this session's companion document, you will find that they are fairly self-explanatory. Some data types are designed for whole numbers, others for regular numbers, and some are specialized, such as those for text and for True/False values. A few of the data types probably require some special attention so that you can understand them fully. Specifically, we need to look at the Date, Object, and Variant data types.Dates and times are both stored in the Date data type, using a serial number technique. This approach means that the portion of the number to the left of the decimal point represents the date, and the portion of the number to the right of the decimal point represents the time. In VBA, the whole portion of a date serial number represents the number of days since January 1, 100. Yes, the year 100. Thus, 0 is the serial number for January 1st, 100. 1 is the serial number for January 2, 100. 2 is for January 3, 100, and so on. These serial numbers increment all the way through whatever huge number represents December 31, 9999. That means that you can represent an amazingly large range of dates in VBA, up to 9999 years.The easiest way to set a Date variable is to use the technique of using the CDate function. Let us say, for instance, that in MyMacro, I had a declaration that said, Dim dMyDate As Date. Then I can use the CDate function to actually assign a date value into that variable. I would do that by just saying, dMyDate = CDate. Then all I would have to do is put the date within parentheses and within quote marks here on this particular line. I would say the month, followed by the day, followed by the year, and then we have assigned that date value to the dMyDate variable. The CDate function converts the date to an actual serial number, and this value is then stored in the dMyDate variable.You can also use another VBA function, called DateSerial, to set a date variable. We could, if we wanted to, say, dMyDate = DateSerial, and in this case, I put the year, followed by the month, and then followed by the day. Notice that those are parameters to the DateSerial function. They are separated by commas and then that will do the assignment of that serial number value to dMyDate as well.Note that both functions expect that the date provided is in a very specific format. In the case of the CDate function, it has to be in month, day, year; and in the case of the DateSerial function, it needs to be in year, month, day. If you try to store it in a different format, then it will not work properly, so you have to pay attention to how you assign it using those particular functions in your coding.The Object data type is another one that may cause a bit of question. Objects are part and parcel of an object-oriented programming language such as Visual Basic for Applications. They are used to access all the pieces and parts that Word is capable of working with in a document. The most likely time that you will utilize objects is when you want to work with an object within Word's object model hierarchy. You will discover more about objects and the Word object model in a different session later in the course.The final data type I wanted to spend some time with right now is the Variant data type. This one gets its name from its ability to vary its format, taking on the form of the data that it contains. It is the chameleon of the data type world. If you store a string in a variable declared as a Variant data type, the variant appears as a String data type and can be manipulated by the string operators. If you store a number to a variant, it acts like a Numeric variable, and you can use the arithmetic operators in its manipulation.While variants are convenient, in the sense that you do not have to think much to use them, I recommend that unless you have a specific need that cannot be solved in any reasonable way without their use, you should avoid them. It is always better to have a thorough understanding of what your program is doing and why it is doing it. Using variants is like painting without first scraping, sanding, and priming. If everything under the new paint is okay, you can get away without any extra work. If it is not, then the new paint is soon a mess, and you are doing the job over.In this session, you have discovered what variables are, what data types are, and how you can declare variables in your macros. In the next session, we will go one step further, and I will introduce you to a way that you can always make sure that you declare the variables used in your macros. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches