Professional Documents
Culture Documents
1. TABLE OF CONTENTS
1. Table of contents________________________________________________________ 2
2. Introduction ____________________________________________________________ 4
3. The Range Property _____________________________________________________ 5
4. The Cells Property of the Worksheet ________________________________________ 7
5. Using Cells and Range together ____________________________________________ 9
6. The Offset Property of Range _____________________________________________ 13
7. Using Rows and Columns as Ranges _______________________________________ 17
8. Using Range in place of Worksheet ________________________________________ 18
9. Reading Values from one Cell to another ___________________________________ 19
10. Reading Values to variables ____________________________________________ 21
11. How to Copy and Paste Cells____________________________________________ 23
12. Reading a Range of Cells to an Array _____________________________________ 25
13. Going through all the cells in a Range ____________________________________ 26
14. Formatting Cells _____________________________________________________ 27
15. Conclusion __________________________________________________________ 28
16. What's Next? ________________________________________________________ 29
17. Free Ebook __________________________________________________________ 29
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 3
"It is a capital mistake to theorize before one has data"- Sir Arthur Conan
Doyle
This eBook covers everything you need to know about using Cells and Ranges in
VBA. It is a pdf version of the post The Complete Guide to Ranges and Cells in Excel
VBA.
If you want information about using Range then check out the The Range Property.
For more information on using the Cells go to The Cells Property of the Worksheet
section.
Other topics included are The Offset Property of Range, Reading Values from
one Cell to another.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 4
2. INTRODUCTION
This is the third post dealing with the three main elements of VBA. These three
elements are the Workbooks, Worksheets and Ranges/Cells. Cells are by far the most
important part of Excel. Almost everything you do in Excel starts and ends with Cells.
1. Read
2. Write
3. Change the format
Excel has a number of methods for accessing cells such as Range, Cells and Offset.
"Why do I need them", "When should you use them?","Which is best ?" are questions
I am often asked.
In this post I will fully investigate each one of these methods of access and provide
you with answers to those questions.
Let's start with the simplest method of accessing cells - using the Range property of
the worksheet.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 5
The following example shows you how to place a value in a cell using the Range
property.
As you can see Range is a member of the worksheet which in turn is a member of the
Workbook. This follows the same hierarchy as in Excel so should be easy to
understand. To do something with Range you must first specify the workbook and
worksheet it belongs to.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 6
For the rest of this post I will use the code name of the sheet. This makes the code
clearer as I will not need to specify the workbook each time. You can use a sheet
directly with the code name as long as it is in the current workbook.
You can see the Code Name of the sheet in the VBAProject window. It is the name
outside the parenthesis.
The following code shows the above example using the Code Name of the worksheet.
I will use the worksheet code name in the rest of the examples. It makes the code
much easier to read.
You can also write to multiple cells using the Range property
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 7
The example below shows you how to write values to cells using both the Range and
Cells property
You may be wondering when you should use Cells and when you should use Range.
Using Range is useful for accessing the same cells each time the Macro runs. For
example, if you were using a Macro to calculate a total and write it to cell A10 every
time then Range would be suitable for this task.
Using the Cells property is useful if you are accessing a cell based on a number that
may vary. It is easier explain this with an example. The following code finds the first
blank cell in the first spreadsheet row and writes text to it.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 8
In this example we have the number of the column and the row. To use Range here
would require us to convert these values to the letter/number cell reference e.g. "C1".
Using the Cells property allows us to provide a row and a column number to access a
cell.
Sometimes you may want to return more than one cell using row and column
numbers. The next section shows you how to do this.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 9
As you can see, you provide the start and end cell of the Range. Sometimes it can be
tricky to see which range you are dealing with when the value are all numbers. Range
has a property called Address which displays the letter/ number cell reference of
any range.
This can come in very handy when you are debugging or writing code for the first
time.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 10
In the following example we print out the address of the ranges we are using.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 11
In the example I used Debug.Print to print to the Immediate Window. To view this
window select View->Immediate Window (or Ctrl G)
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 12
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 13
With the Offset property you can get a Range of cells the same size and a certain
distance from the current range. The reason this is useful is that sometimes you may
want to select a Range based on a certain condition.
For example in the screenshot below there is a column for each day of the week.
Given the day number (i.e. Monday=1, Tuesday=2 etc.) we need to write the value to
the correct column.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 14
As you can see in the example, we need to add a line for each possible option. This is
not an ideal situation. Using the Offset Property provides a much cleaner solution
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 15
As you can see this solution is much better. If the number of days in increased then
we do not need to add any more code. For Offset to be useful there needs to be some
kind of relationship between the positions of the cells. If the Day columns in the
above example were random then we could not use Offset. We would have to use the
first solution.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 16
One thing to keep in mind is that Offset retains the size of the range. So
.Range("A1:A3").Offset(1,1) returns the range B2:B4. Below are some more examples
of using Offset.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 17
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 18
The code below will set the second column of the range to bold. As the range has only
two rows the entire column is considered B1:B2
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 19
To write data from one cell to another we do the same. The destination range goes on
the left and the source range goes on the right. The following example shows you how
to do this
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 20
As you can see from this example it is not possible to read from multiple cells. If you
want to do this you can use the Copy function of Range with the Destination
parameter
The Copy function copies everything including the format of the cells. It is the same
result as manually copying and pasting a selection. You can see more about it in the
How to Copy and Paste Cells section.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 21
A variable is used to store values while a Macro is running. You normally do this
when you want to manipulate the data before writing it somewhere.
The following is a simple example using a variable. As you can see the value of the
item to the right of the equals is written to the item to the left of the equals.
You can write a variable to a range of cells. You just specify the range on the left and
the value will be written to all cells in the range.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 22
You cannot read from multiple cells to a variable. However you can read to an array
which is a collection of variables. We will look at doing this in the next section.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 23
1 Range("A1:B4").Copy Destination:=Range("C5")
Using this method copies everything values, formats, formulas and so on.
If you want to copy individual items you can use the PasteSpecial property of
range. It works like this
1 Range("A1:B4").Copy
2 Range("F3").PasteSpecial Paste:=xlPasteValues
3 Range("F3").PasteSpecial Paste:=xlPasteFormats
4 Range("F3").PasteSpecial Paste:=xlPasteFormulas
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 24
The following table shows a full list of all the paste types
PASTE TYPE
xlPasteAll
xlPasteAllExceptBorders
xlPasteAllMergingConditionalFormats
xlPasteAllUsingSourceTheme
xlPasteColumnWidths
xlPasteComments
xlPasteFormats
xlPasteFormulas
xlPasteFormulasAndNumberFormats
xlPasteValidation
xlPasteValues
xlPasteValuesAndNumberFormats
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 25
You can read data from a range of cells to an array in just one line of code. You can
also write back to a range of cells from an array in just one line. This is a remarkably
efficient way of getting a lot of values in one go. It saves you from having read the
cells one at a time.
Keep in mind that the array created by the read is a 2 dimensional array. This is
because a spreadsheet stores values in two dimensions i.e. rows and columns
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 26
You can also go through consecutive Cells using the Cells property and a
standard For loop. The standard loop is more flexible about the order you use but it
is slower than a For Each loop.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 27
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 28
15. CONCLUSION
The following is a summary of the main points
1. The Range property of the Worksheet can be used to return a range of Cells.
It takes the number/letter reference e.g. A1:A56. It is best used when the
range will be the same each time the macro is run.
2. Use the Cells property of the Worksheet when the cell location is variable.
Cells return a range of one cell.
3. Use Range(Cells,Cells) to return more than one cell using
the Cells property.
4. Offset is used when the range is relative based on a condition e.g. the day of
the week.
5. You can use Columns and Rows when you require the entire column or row.
6. Cells, Columns and Rows are also members of Range but should be only
used this way if there is a specific need.
7. You can read from one cell to another by placing the destination cell on the
left of an equals sign and the source cells on the right.
8. You can write the value from one cell or variable to a range of cells in one line.
9. You cannot read from a range of cells in one line. To copy from multiple cells
you can use the Copy function with the Destination parameter.
10. You can read values from cells to variables and vice versa. You can write a
variable to a range of cells in one line.
11. Using arrays you there is a super-efficient way of reading from a Range of cells
to an array using just one line.
12. You can also write from an array to a range of cells in just one line.
13. You can use a For Each loop to run through every cell in a range.
14. You can also use a normal For loop. It is slower than For Each but allows
more flexibility on the order.
15. Formatting cells is straightforward once you have the range.
ExcelMacroMastery.com
THE COMPLETE GUIDE TO RANGES AND CELLS IN EXCEL 29
You can view a list of all the VBA posts on my blog here.
If you would like to receive a free eBook and exclusive content not available on my
blog then you can sign up here
ExcelMacroMastery.com