Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

please help

66 views
Skip to first unread message

mrmac

unread,
Aug 15, 2003, 11:38:27 PM8/15/03
to
I'm am trying to programmatically select several sheets
in a workbook. The recorded macro looks like this:

sub macro1()
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
end sub

The macro I wrote to mimic that, gives me a Subscript out
of range error. What am I doing wrong?

Sub macro()
Dim chrts as String
chrts = Chr(34)
For x = 1 To Sheets.Count
chrts = chrts & Sheets(x).Name & Chr(34) & Chr(44) _
& Chr(32) & Chr(34)
Next x
chrts = Left(chrts, Len(chrts) - 3)
Sheets(Array(chrts)).Select
End Sub

Any help is really appreciated.

Rob Bovey

unread,
Aug 16, 2003, 12:06:50 AM8/16/03
to
To answer your immediate question, you don't need to mimic the Array
function. Just use a real array loaded with the names of all the sheets you
want to select as follows:

Sub SelectSheets()
Dim lCount As Long
Dim szNames() As String
For lCount = 1 To ThisWorkbook.Sheets.Count
ReDim Preserve szNames(1 To lCount)
szNames(lCount) = ThisWorkbook.Sheets(lCount).Name
Next lCount
ThisWorkbook.Sheets(szNames()).Select
End Sub

However, if you just want to select all of the sheets in a workbook, this
would be a lot easier:

ThisWorkbook.Sheets.Select

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"mrmac" <delete...@yahoo.com> wrote in message
news:06ad01c363a7$da8ba920$a301...@phx.gbl...

mrmac

unread,
Aug 16, 2003, 11:41:03 AM8/16/03
to
Thank you Rob. It is a lot simpler than I thought.

FYI. The deal is to copy all charts from a wb to a new
wb, then to copy a subset of this to a new wb. Then close
these wbs, delete most of the charts from the original wb
and create new charts from different data. This is done
12 times in a loop and the number of charts created in
each loop is variable. This is the piece I was missing.
Thanks for your timely help.

>.
>

0 new messages