星期五, 九月 02, 2011

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

From Chandoo.org
How to use Cells, Ranges & Other Objects in Excel VBA:

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 Chandoo.org 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.


Tomorrow, we will put together all these lessons to create a Daily Sales Tracker for our “We Are Nuts” dry fruit & nuts chain of stores.


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 Chandoo.org’s VBA Classes


We run an online VBA (Macros) Class every 3 months. We just finished our first batch and it was an awesome experience for both students & teachers (Vijay, Hui and I teach this course). Now, we are opening the enrollments for 2nd batch of VBA classes from September 5th, Monday. If you are interested to learn VBA and become a master in it, please consider joining this course.


Click here to learn more about our VBA program.




没有评论: