Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel 2010 VBA - Difference between activating a cell and selecting

Posted on 2013-11-09
9
Medium Priority
?
1,795 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 664 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 81

Assisted Solution

by:byundt
byundt earned 1336 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 664 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
 
LVL 81

Assisted Solution

by:byundt
byundt earned 1336 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 81

Accepted Solution

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

Assisted Solution

by:byundt
byundt earned 1336 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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