Solved

Excel 2010 VBA - Difference between activating a cell and selecting

Posted on 2013-11-09
9
1,266 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 81

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 81

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 81

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 81

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 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

19 Experts available now in Live!

Get 1:1 Help Now