Excel VBATutorial                      

             

Excel VBA Macros

Email to excel-vba.com

Call
613-749-4695

Click to Email
[email protected]

Or let's have a live conversation computer to computeer for free:

Skype ID:
peter-excel-vba

Excel Tutorial on Macros

Excel Consulting

Here is a sample of what you will find in lesson 19 of the downloadable Tutorial on Excel macros

Lesson 19 on Excel Macros (VBA):

VBA Code for Variables

You will start developing complex and sophisticated programs in Excel and you will start working with very large sets of data when you discover the variables.

A variable is an object that you create and in which you can store text, dates, numbers or almost anything else. Why should you use variable? The first good reason is to make your code dynamic, to avoid hard coding some values.

Hard Coding vs Dynamic Coding

You are hard coding when you write:
Workbooks.Open "MyFile.xls"

You are dynamically coding when you enter the name of the file in an cell (A1) of your Excel sheet and you write.
varWorkbook=Range("A1").Value
Workbooks.Open varWorkbook

At this point you or the user can change the name of the workbook to open in cell A1 instead of going to the VBA code in the Visual Basic Editor.

You will also create variables to count the number of rows, store the result in a variable and then do something as many time as there are rows.

For varCounter = 1 to varNbRows
        Selection.Value=Selection.Value*2
        Selection.Offset(1,0).select
Next

In the VBA procedure above the value in each cell is multiplied by 2 then the cell below is selected. This action is repeated as many times as there are rows in the set of data.


We hope you have enjoyed this introduction to lesson 19
for more on this topic and a complete course on Excel macros download the
Tutorial on Excel Macros


Next Lesson: VBA Code for Statements