Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Excel VBA actions are possible on hidden worksheets or workbooks?

Tags:

excel

vba

Hidden worksheets/workbooks have some limitations to what can be done in VBA code, like most Select and Selection statements, and anything coming from ActiveSheet, but I can't seem to find any list of what the limitations are.

Google, the built-in documentation in the help system, and MSDN's website have all failed me. Can anyone point me in the right direction?

Edit: The workbook is opened with

Set WB_Master = Workbooks.Open(Filename:=PATH_Master, ReadOnly:=False)

and then hidden with

WB_Master.Windows(1).Visible = False
like image 864
Andrew Scagnelli Avatar asked May 21 '09 12:05

Andrew Scagnelli


1 Answers

From the Visual Basic for Applications help:

When an object is hidden, it's removed from the screen and its Visible property is set to False. A hidden object's controls aren't accessible to the user, but they are available programmatically to the running application, to other processes that may be communicating with the application through Automation, and in Windows, to Timer control events.

Not much help there I'm afraid, and I couldn't find much else through Google.

As you said yourself, the Select method and Selection Property don't work on a hidden Worksheet, they should work on a hidden Workbook though. (Please correct me if I'm wrong.) In general however, it's not always all that efficient to select ranges in worksheets anyway, you are better off working with the Range property (which does work on a hidden worksheet).

EDIT:

The following code will change the color of A1:A8 to Cyan even when the Worksheet is not visible:

Dim book2 As Workbook
Set book2 = Workbooks.Open("C:\Book2.xls")

book2.Worksheets("Sheet1").Visible = False
book2.Windows(1).Visible = False

With book2.Worksheets("Sheet1").Range("A1:E8").Interior
    .ColorIndex = 8
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
End With

book2.Windows(1).Visible = True
book2.Worksheets("Sheet1").Visible = True
like image 76
Patrick McDonald Avatar answered Sep 19 '22 13:09

Patrick McDonald