Excel 2010 VBA - Difference between activating a cell and selecting

What is the difference between:

In both cases my cell pointer is placed in cell F2.  I see no difference.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Activate works only on a single cell, which much reside in the current selection, and brings the cursor in that cell

Select can select any range, but does not let you define which cell has the cursor.
Steven HarrisPresidentCommented:
As JamesBurger said, "many cells can be selected, but only one may be the active cell at any given time."

Objects that are selected are also placed in the Selection object which you can use methods on.

As a best practice, is better to avoid both if at all possible.  It is better to work directly on a Range that to select it or activate it first.
byundtMechanical EngineerCommented:
Although there is a difference between activating and selecting a cell, your code shouldn't be doing either. In almost every situation, you don't need to activate or select a cell in order to read or write its formatting and other properties, change its formula or values, cut, copy or paste. The same thing holds true for activating a worksheet, workbook or window. You don't need to do it.

I realize that the macro recorder selects and activates like crazy, but that doesn't make it a good practice. Selecting or activating a cell will cause the screen to blink, slows down your macro enormously, and frustrates the user by moving the cursor all over kingdom come. Learn how to avoid doing it.

One of the very few exceptions to the good practice of "never selecting a cell" is when you manipulate the conditional formatting properties of a cell using formula criteria with relative addresses. The formula in such situations is interpreted relative to the active cell, so you will get unexpectedly wrong results if you don't select the cell you are trying to format first.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Jacques Bourgeois (James Burger)PresidentCommented:
There is a very good reason to select or activate a cell or a range: you might want to put the cursor or set the selection to where the user might need to work after a series of programmatic operations.
byundtMechanical EngineerCommented:
I use Application.GoTo in the circumstance described by JamesBurger because it can change both worksheet and range in a single statement.

For example, I set a "return address" before opening a workbook or adding a worksheet. Both of those activities will change the active cell and active worksheet. After the macro is done processing, I use Application.GoTo to return to the original active cell.
Dim celHome As Range
Set celHome = ActiveCell
'Open a workbook, add a worksheet or other activity that changes the active worksheet
Application.GoTo celHome

Open in new window

brothertruffle880Author Commented:
This is an absolutely fascinating and informative thread.

The consensus is that I should NOT use select or activate.

But it raises a question.  If I don't use select or activate, what should I use instead?

Let's say I have a simple worksheet that I need to have a formula placed in (see attached worksheet.  This is a simple "value of inventory" problem.  How could I code this solution in VBA without using select or activate?
byundtMechanical EngineerCommented:
You would use a statement like:
Worksheets("Sheet1").Range("D2:D5").Formula = "=B2*C2"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
byundtMechanical EngineerCommented:
In the suggested VBA statement, I assumed that the workbook needing the formula was already active. If not, I would preface the statement with a reference to the workbook:
Workbooks("no-selection-ee-question.xlsx").Worksheets("Sheet1").Range("D2:D5").Formula = "=B2*C2" 

Open in new window

What you want to do is to set the Formula property of a Range object. This Range object can be a variable that was previously defined, or it can be a fully qualified reference to a range of cells. By fully qualified, I mean that you make it clear which workbook, worksheet and cells are involved.

The initial example, which is how I would do it except when I am copying back and forth between two different workbooks, VBA assumes that the worksheet reference is to the active workbook.

Another way to do is using a variable. Suppose that you want to do a number of tasks with worksheet Sheet1. You might set a variable pointing to it:
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
ws.Range("D2:D5").Formula = "=B2*C2"
ws.Name = "Billings Summary"

Open in new window

The power of a variable is exposed when you want to loop through different items in a collection. Suppose, for example that you wanted to put the valuation formula in every worksheet in the workbook. To do that, you might use code like:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    ws.Range("D2:D5").Formula = "=B2*C2"

Open in new window

In the preceding loop, the code doesn't care about the names of the worksheets. It doesn't even care how many worksheets there are. Yet the macro recorder would insist that you go through the process of selecting each sheet, then selecting a range before putting the formula in those cells.
brothertruffle880Author Commented:
Wow.  This thread was  intense!
Thanks to all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.