- Business and Employment»
- Learn Business Skills
Creating a UserForm in Microsoft Excel - Step 2 - Preparing the Table (A Dynamic Range)
This is the the second Hub of the series: "Creating A Userform in Microsoft Excel". In this series we will walk through the steps of creating a simple address book that will be managed via a userform.
The Table Behind the Scenes
Now that you have accessed the Visual Basic Editor, go ahead and minimize it because we won't use it just yet.
In this Hub, we will create the table that will house our address book.
For this simple address book we will include the following fields:
- Last Name
- First Name
- Address Line 1
- Address Line 2
- Zip Code
- Christmas Card (Y/N)
Simply put those fields as column headings on the top row of Sheet1.
Address Book - Table
Creating a Dynamic Range
Creating the table in the sheet is really easy but giving it an official name that excel will recognize is a little trickier. Naming ranges is something that you can do in excel to make writing formulas a little easier. For example: Let's say that you wanted to add "Cost" (located in Cell "A1") and "Tax" (located in cell "A2") together to get "Total Cost". One way to achieve this would be to write a formula like this:
That would work well, but if your formula were more complex it might be nice to reference the variable by name rather than its cell location. To do this you would create a "Named Range" for each of the variables. You do this using the "Name Manager".
To access the Name Manager, click on the Formulas Tab and then click on Name Manager.
Within the Name Manager you specify a range (a single cell or a group) and give it a name. You can then use those names rather than the cell's locations. The example formula I gave before could then be written:
for our address book, we will need to reference our list of addresses using VBA so we want to give it a name. The only problem is that we don't know exactly how big our list will be. In fact its size may change on a daily basis. To handle this we need to create a "Dynamic Range". We will do this by using the offset function and a nested Counta function. I'll show you the function, then walk you through it.
=OFFSET(Reference, Rows, Cols,[Height],[Width])
Offset tells excel that we want to give a starting point but ultimately use a located a specified distance away from that starting point.
Reference is that starting point. For our address book we want to start in the upper left-hand corner which is cell A1 on Sheet1. (When writing the formula, just click in cell A1 and it will automatically put in the dollar signs and exclamation point that you need to make it work right.)
Rows is how many rows from the reference you want your range to begin. In our case we want to go down one row, because that is where our first address will be. So we use "1".
Cols is how many columns from the reference you want your range to begin. In our case we want to start on the same column as our reference so we won't move any. "0".
If we were just referencing a single cell, we would be done which is why Height and Width are in brackets (they are optional). However, we want to reference a multi-cell range so we will include the Height and Width.
Height is the number of rows your range includes. Since this number will vary, we will use the Counta function. Counta counts the number of cells in a range that aren't blank. The range I have referenced for Counta is the entire A column. If we have three addresses in our table then counta will give us 4 because it counts the 3 addresses and the 1 column header.
Width is the number of columns your range includes. In our case, our Address Book table (range) has 9 columns.
So now we have a range that will grow and shrink depending on the number or addresses we have.
Go ahead and open the Name Manger and click "New". In the box that appears type in the name that you want to give your range. I chose "Address_Book". I used the underscore (_) because the name of a range can't have any spaces.
In the "Refers to:" box, type our formula. Remember, you can click in the cells that you want to reference and it will input the appropriate text. Once the formula is complete click "OK". When the Name Manager appears again, click on the "Reference" icon to the right of your formula to test it out. It will display the range you have specified.
A Dynamic Range
Join me in my next Hub in this series "Creating a Userform in Microsoft Excel" where we will return to the Visual Basic Editor and lay out the userform that will be used to manage our Address Book.