Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Excel sheets have to be activated before selection?

This code

Sheets(1).Activate
Sheets(2).Range("A1").Select

will fail in VBA because you can only use Select on an object which is Active. I understand this is the case.

What element of the Excel datamodel causes this to be the case? I would think there is an implicit intent from a user/coder to Activate any object immediately prior to using Select - I do not understand why VBA would not make this assumption, and, I am assuming there is a reason this distinction exists.

  • What part of Excel's datamodel prevents selection without activation?
like image 457
enderland Avatar asked Oct 02 '12 23:10

enderland


People also ask

Can you select a worksheet without activating it?

If a worksheet is not the active sheet, it cannot have a selection nor an activecell. There is one Selection for the entire Excel application instance regardless of how many workbooks or worksheets or windows are open.

How do I activate selected worksheets in Excel?

By keyboard: First, press F6 to activate the sheet tabs. Next, use the left or right arrow keys to select the sheet you want, then you can use Ctrl+Space to select that sheet. Repeat the arrow and Ctrl+Space steps to select additional sheets.

What does it mean to activate a sheet?

What does it mean to activate a sheet? The active sheet is just the sheet that is currently open. In the spreadsheet below, the active sheet is a sheet called Step 2. You can activate a sheet by manually opening it or programmatically using Apps Script.

What does activate mean in Excel?

The Activate method allows us to select a single object. This can be a single object within a selection, if multiple objects are already selected. The following lines would select the three sheets, then make Sheet3 the active sheet that the user sees.


2 Answers

As brettdj pointed out, you do not have to activate the sheet in order to select a range. Here's a reference with a surprisingly large amount of examples for selecting cells/ranges.

Now as for the why do I have to active the sheet first? I do not believe it is a fault of the datamodel, but simply a limitation of the select method for Ranges.

From experimentation, it looks like there are two requirements to select a range in Excel.

  1. Excel must be able to update the UI to indicate what is selected.
  2. The ranges parent (I.E. the sheet) must be active.

To support this claim, you also cannot select a cell from a hidden sheet.

Sheets(1).Visible = False
Sheets(1).Activate
'The next line fails because the Range cannot be selected.
Sheets(1).Range("A1").Select

Simply put, when it comes to Ranges, you cannot select one you cannot see.

I would have claimed this is a limitation of select all together, except that you can actually select an object in a hidden sheet. Silly Excel.

like image 146
Daniel Avatar answered Nov 14 '22 21:11

Daniel


I know that this is a bit late to the party, but I discovered a hack to do this...

Try this code:

Sheets(1).Activate
Sheets(2).Range("A1").Copy
Sheets(2).Range("A1").PasteSpecial xlPasteFormulas
Application.CutCopyMode = False

Note that it is a hack, but it does the trick!!

like image 38
John Bustos Avatar answered Nov 14 '22 23:11

John Bustos