Solved

Excel 2010 VBA - Difference between activating a cell and selecting

Posted on 2013-11-09
9
1,236 Views
Last Modified: 2013-11-17
What is the difference between:
Range("f2").Activate
and
Range("f2").Select


In both cases my cell pointer is placed in cell F2.  I see no difference.
0
Comment
Question by:brothertruffle880
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 39636439
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.
0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39636450
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.
0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 334 total points
ID: 39636452
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.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 166 total points
ID: 39636470
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 80

Assisted Solution

by:byundt
byundt earned 334 total points
ID: 39636472
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

0
 

Author Comment

by:brothertruffle880
ID: 39652182
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?
no-selection-ee-question.xlsx
0
 
LVL 80

Accepted Solution

by:
byundt earned 334 total points
ID: 39652213
You would use a statement like:
Worksheets("Sheet1").Range("D2:D5").Formula = "=B2*C2"
0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 334 total points
ID: 39652270
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"
Next

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.
0
 

Author Closing Comment

by:brothertruffle880
ID: 39655306
Wow.  This thread was  intense!
Thanks to all.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now