PC Review


Reply
Thread Tools Rate Thread

Code to select all worksheets (variable number of sheets)

 
 
OssieMac
Guest
Posts: n/a
 
      27th May 2009
I want to do the following but with a variable number of worksheets.

Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select

or the following.

Sheets(Array(1, 2, 3)).Select

I found similar code to the following from this forum and it was checked as
an answer but I am not having any success. See the comment in the code for
the error.

Sub SelectMultipleSheets()

Dim intShtsCount As Integer
Dim shtArray()
Dim i As Integer

intShtsCount = ActiveWorkbook.Sheets.Count
ReDim shtArray(intShtsCount)
For i = 0 To intShtsCount - 1
shtArray(i) = Sheets(i + 1).Name
Next i

'*******************************
'Following line returns runtime error '9' Subscript out of range
'********************************
Sheets(shtArray).Select

End Sub

All help appreciated.

--
Regards,

OssieMac
 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      27th May 2009
The problem you are having is that you are declaring the array with
one extra element, with the code

ReDim shtArray(intShtsCount)

Since your array is 0-based, the last element of the array will be
empty. E.g., with three sheets, you'll have

shtArray(0) = Sheet1
shtArray(1) = Sheet2
shtArray(2) = Sheet3
shtArray(3) = empty

Use code like

ReDim shtArray(intShtsCount - 1)

As a matter of general good coding practice, you should always specify
the lower bound of the array. E.g., instead of

ReDim shtArray(intShtsCount)

Use

ReDim shtArray(0 To intShtsCount - 1)

The reason is that the default lower bound of an array is either 0 or
1, depending on the existence of and value of the Option Base
directive at the top of the module:

Option Base 0 -> arrays begin at element 0 unless specified
Option Base 1 -> arrays begin at element 1 unless specified
no Option Base statement -> Option Base 0 assumed.

If you copy the code from one module to another, the lower bound of
the array may be changed from 0 to 1. Thus, always specify the lower
bound.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 27 May 2009 15:06:01 -0700, OssieMac
<(E-Mail Removed)> wrote:

>I want to do the following but with a variable number of worksheets.
>
>Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
>
>or the following.
>
>Sheets(Array(1, 2, 3)).Select
>
>I found similar code to the following from this forum and it was checked as
>an answer but I am not having any success. See the comment in the code for
>the error.
>
>Sub SelectMultipleSheets()
>
>Dim intShtsCount As Integer
>Dim shtArray()
>Dim i As Integer
>
>intShtsCount = ActiveWorkbook.Sheets.Count
>ReDim shtArray(intShtsCount)
>For i = 0 To intShtsCount - 1
> shtArray(i) = Sheets(i + 1).Name
>Next i
>
>'*******************************
>'Following line returns runtime error '9' Subscript out of range
>'********************************
>Sheets(shtArray).Select
>
>End Sub
>
>All help appreciated.

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      28th May 2009
Thankyou Chip. I am very greatful for your help.
However, I am suitably embarrassed. Not only could I not see the error for
which I knew the answer, I didn't test UBound(shtArray) which would have
immediately indicated what the error was. I simply put a MsgBox in the
For/Next loop to ensure it was picking up all the sheets.

--
Regards,

OssieMac


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Summing VLookup from a variable number of sheets Allen Microsoft Excel Misc 4 20th Jan 2010 03:32 AM
Range of cells from variable number of work sheets. sp107 Microsoft Excel Programming 0 20th Oct 2009 03:36 PM
++HELP ME PLEASE++ ComboBoxes as a Variable to Select Sheets Adeptus - ExcelForums.com Microsoft Excel Programming 14 10th Jul 2005 04:49 AM
Re: How to use a variable as a sheets code name Rodney POWELL Microsoft Excel Programming 0 25th Aug 2004 04:55 PM
Moving a variable number of Sheets to another Workbook Nelson M Microsoft Excel Programming 2 16th Jan 2004 03:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.