How to use Cells, Ranges & Other Objects in Excel VBA

Posted on August 31st, 2011 in VBA Macros - 15 comments

This article is part of our VBA Crash Course. Please read the rest of the articles in this series by clicking below links.

Using Cells, Ranges & Other Objects in VBA Macros

  1. What is VBA & Writing your First VBA Macro in Excel
  2. Understanding Variables, Conditions & Loops in VBA
  3. Using Cells, Ranges & Other Objects in your Macros
  4. Putting it all together – Your First VBA Application using Excel
  5. My Top 10 Tips for Mastering VBA & Excel Macros

In part 3 of our VBA Crash Course, we are going to learn how to speak with various Excel objects like Cells, Ranges etc. and deal with them.

Objects – what are they?

Any thing and everything is an object. Your dog, your bed, your neighbors cat, their car, your bike, your computer, the shiny new Excel workbook you just created, my website, your email account – every thing is an object. For that matter, Lady Gaga’s meat dress is an object too. But that is a whole different subject.

From our “We are nuts” example yesterday, you can already see these objects:

  • One awesome owner (that is you)
  • 24 store manager objects
  • 24 store objects

Some sample objects you can find in Excel workbooks

  • Cells, lots of them
  • Ranges of cells
  • Worksheets
  • Charts
  • Pivot Tables
  • The entire workbook

Objects & Excel VBA

Since your Excel workbook is nothing but a collection of objects, whenever you want to make any change (like modify a cell’s value or recolor a chart), you need to refer to the corresponding object and do the necessary thing.

But how do we talk to these objects from VBA. Well, to know that, you must understand how an Object looks to our eyes vis-a-vis computer’s eyes. Here is an illustration to help you understand the difference.

Understanding Excel Objects and using them - How we perceive an object & how computer perceives it.

As you can guess, Objects have Properties. In the case above, color RED is a property of the cell object.

Objects – What are they made of?

In VBA world, objects are made of 2 things – properties & methods.

The color of a cell is a property.

You use copy method to copy cell’s value to Excel’s clipboard.

In other words, properties are what an object has. Methods are operations you do on the object.

Note: Certain objects also have a special class of methods called as Events. An Event is a special type of method that runs only when a circumstance is met. For example, select a cell, Excel internally runs SelectionChange Event on the current worksheet.

How do we access these properties & methods?

In plain English, if you want to know the color of a cell, you would ask “What is the color of cell A4?”

In Excel VBA language, the same becomes Range(“A4”).Interior.Color

Notice how the dot (.) is used like of in our plain English version.

Dot (.) is your best friend when dealing with objects. Since many Excel objects have dozens of properties and methods associated with them, to help us understand and use right properties, VBE (Visual Basic Editor) shows all the properties and methods whenever you press . after typing an object, like this:

Object Properties & Methods are shown when you press . in VBE

Most commonly used Objects in VBA:

While there are no single set of objects that are used by everyone for every need, there are a few VBA objects that are used in many situations. In this section, we will examine these objects.

Range Object:

Range object is used to refer to a range of cells. For example Range(“A1:A10”) refers to the cells A1:A10 in the current worksheet. Range has a lot of useful methods and properties. One of the commonly used property is is Range(“A1:A10”).cells which refers to all the cells in the range.

Tips on using range object:

  • You can use named ranges in Range object like this: Range(“myStoreList”)
  • You can use square brackets [] to refer to ranges like this: [A10] refers to Range(“A10”)
  • You can use variables in Range Object like this: Range(mylist) refers to whatever address is stored in mylist variable.
  • You can use variables along with static text, like this: Range(“A1:A” & endPoint) refers to the range A1:A120, assuming endPoint variable is 120.

ActiveCell Object:

Active cell object refers to the currently selected cell. If you have selected a range of cells, usually ActiveCell refers to the top-left cell.

Tips on using ActiveCell Object:

  • Use ActiveCell.End(xlDown).Activate to select the last cell in the same row with a value (assumes you have no breaks in between). You can also use options like xlToLeft, xlToRight, xlUp too.

Selection Object:

Selection object refers to the currently selected cells or anything else that is selected (like a chart or drawing shape).

How to learn about various Excel Objects and use them:

Excel has a lot of objects. Some times objects contain other objects. For example a Range contains some Cells. A Selection may contain some Charts. Understanding the hierarchy and properties of all these objects is a tough task. But thankfully, there is help.

Here are my top tips to learn about various Excel Objects:

  • Use Macro Recorder: Whenever you need to use an object that you are not familiar with, just use built-in macro recorder and do some operations on that object. Now stop it and view the code. You would have a good idea how to deal with that object. For ex. if you want to learn how to use VBA to refresh a pivot table, just start recorder, select the pivot, refresh it and stop the recorder. Now go and see the code and you will have a good idea how to refresh pivot tables from VBA.
  • Use VBA Help: Excel VBA has a very good help system. Just go to Visual Basic Editor (ALT+F11) and press F1 to start the help. Type the object name you want help on and read thru the pages to learn. VBE also has a helpful screen called “Object Browser” to visually browse various Excel objects and understand the methods & properties.
  • Learn from Code Examples: There are several sites, including that publish frequent articles, code samples and tips on Excel & VBA. Follow a handful of these sites and learn from the shared examples.
  • Take up some project: In your day to day work, you always see some problems that can be solved with VBA. So go ahead and take up one such task and try to do it using VBA. This is a great way to learn a new language like VBA.
  • Join a Training Program: Last but not least, joining a training program is a good way to learn VBA. If you want a good program on VBA, consider joining our upcoming batch of VBA Classes.

Putting it all together – a Daily Sales Tracker for “We Are Nuts”:

So far you have learned What is Excel VBA, How to use variables, conditions & loops and How to use various Excel Objects.

In the next part, learn how to create a VBA Application combining all the things you learned so far.

What are your tips for learning about Excel Objects & Using Them?

Excel Object Model is vast and deep. There are a lot of things that we can learn (and remember), but there are a lot more that we will never know or memorize until we need to use them. I always rely on built-in macro recorder to learn about the objects and then modify the code until it works just right.

What about you? How do you learn about Excel Objects? Please share your tips & ideas using comments.

Join Our VBA Classes

We run an online VBA (Macros) Class to make you awesome. This class offers 20+ hours of video content on all aspects of VBA – right from basics to advanced stuff. You can watch the lessons anytime and learn at your own pace. Each lesson offers a download workbook with sample code. If you are interested to learn VBA and become a master in it, please consider joining this course.

Click here to learn more and Join our VBA program.

Introducing our Online Power BI Class:

Introducing Online Power BI Training from - check it out today

Would you like to join me on a date with Power BI? In this comprehensive online class, learn all about Power BI so you can create beautiful, insightful & interactive reports. Join me and rest of the play mates for our first ever Power BI Play Date.

Click here to know more and join us.

Written by Chandoo
Tags: , , , , , ,
Home: Main Page
? Doubt: Ask an Excel Question

15 Responses to “How to use Cells, Ranges & Other Objects in Excel VBA”

  1. Luke M says:

    Along with the VBA help tip, if you type a word in the VBE, place the cursor in it (on it?) and then hit F1, you'll be taken to the article related to that word. Very helpful if you got some code from somebody else and want to know what's going on.

  2. Chandoo, I've been working with VBA for excel for a few years now, and you just taught me some things in this post. Thanks for the great post. I'd be tempted to take your class, but getting ready for a position change, so right now isn't the best time for me. I'll keep you in mind for later.


  3. vijay says:

    hai chandoo, i am new to vba, nice to learn the way ur r teaching.

  4. RAGHAVAN says:

    thank you for the tips . by the by i want a small help from you

    though you have lot of trining modules  i am not in a position to nroll because  very minimal time is available to me after ehausting all my tim in th work place and in th domesic front

    i wud like tos ek help fr u on ase to case basis

    often in vba  formulas are entered as  r1c1 mode/style

    is thr any way we  can  write formulas in vba  exactly as in excel

    by putting he formulas in braces  [  a1 = b1+d2  ]

    where  a1 b1 c2 are cell  references

    challange no 2

    imagine a row of cells contain a formula   which was  constructed in
    named cell  mode    that is  interest  =  pricipal  * rate

    where interest denotes   range name
    principal  denotes named range
    rate  denotes named range

    now when i copy  this row into th row beneath it  it copies only in absolute mode  by repeating the fomula  ditto   BUT IT DOES NOT COPY IN RELATIVEREFERENCE  please  gift  us with a solution



    ca u please eslaborate on this

    Thanks and regards

  5. [...] out this site: Chapter 5: Using Ranges Therea re other helpful hints and tips there too. Also: How to use Cells, Ranges & Other Objects in Excel VBA [VBA Crash Course - Part 3 of 5] | Chandoo... If you have any questions about the code, do not hesitate to ask, but please post the whole code, [...]

  6. Trevor King says:

    You use Store and Sales as if they were part of the VBA. Are these named cells that your are referencing? Or does the VBA understand to read the titles....

    "Sub Store()
    'when you run this macro, it will take the sales of all the 24 stores we own
    'it will ask for a reason if the sales are too low or too high

    Dim storeNum As Integer
    Dim reason As String
    Dim Store As Range

    storeNum = 1
    For Each Store In Range("C3:C5")
    Store.Value = InputBox("Sales for Store " & storeNum)
    If Store.Value 5000 Then
    reason = InputBox("Why are the sales deviated?", "Reason for Deviation", "Reason for Deviation")
    Store.Offset(, 1).Value = reason
    End If
    storeNum = storeNum + 1
    Next Store
    End Sub"

  7. SpreadSheetNinja says:

    sub marine()
    range("A1") = "Meow"
    cells(2,1) = "Meow"
    [A3] = "Meow"
    end sub

  8. Mohammad Shoaeb says:

    This is first time that I decided to learn about VBA I searched the web, found your effort, for the first time I was happy that I found my concern but After few minutes I recognized that my happiness was for knowing smart sincere friend like you.

    with all my love

    Mohammad Shoaeb

  9. […] How to use Cells, Ranges & Other Objects in Excel VBA [VBA … – You use Store and Sales as if they were part of the VBA. Are these named cells that your are referencing? Or does the VBA understand to read the titles……. […]

  10. […] explained by Chandoo (probably the most prominent Excel blogger) here, the ActiveCell and Selection objects which I highlight above are among the most […]

  11. […] How to use Cells, Ranges & Other Objects in Excel VBA [VBA … – In part 3 of our VBA Crash Course, we are going to learn how to speak with various Excel objects like Cells, Ranges etc. and deal with them. Learn about […]

  12. Tati says:

    Hi, I need to create a column that identifies from a set of products which one was bought first. For example, A1:A10 has the same client name, and column B1 has prod X, B2 has prod Y etc.
    Column C has the date each of the products were bought.
    I need to create a chart that shows if a particular product is usually bought first. Thank you in advance for your help!

Leave a Reply