Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Can I include noncontiguous worksheet in array of worksheets?

The following code selects contiguous worksheets 5 through 11.

============

Sub SelectSheets()

Dim Sh() As Variant, wks As Worksheet, i As Integer

i = 0

For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2") = 1 Then
i = i + 1
ReDim Preserve Sh(1 To i)
Sh(i) = wks.Name
End If
Next
Worksheets(Sh()).Select

End Sub

===============

I would like to add one noncontiguous worksheet (#32) to the array of
selected worksheets. Is there a way to do so?


Barney Byrd


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default Can I include noncontiguous worksheet in array of worksheets?

Hi, try this...
Worth noting the If you absolutely want the Array to start at 1 not 0
then move the line i=i+1 above the Redim Preserve Statement..

Sub SelectSheets()
Dim Sh() As Long, wks As Worksheet, i As Integer
For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2").Value = 1 Or wks.Index = 32 Then
ReDim Preserve Sh(i) '''No Need for 1 to i.
'''If you want your arrays to start at 1 then you can use Option Base 1
'''in Declarations section of code
Sh(i) = wks.Index
i = i + 1
End If
Next
Worksheets(Sh()).Select
End Sub


Hth,
O

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Can I include noncontiguous worksheet in array of worksheets?

Barney,

Something like this...
'---------------------------------
Sub SelectSheets()
Dim Sh() As String
Dim wks As Worksheet
Dim i As Long

ReDim Sh(1 To Sheets.Count)

For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2") = 1 Then
i = i + 1
Sh(i) = wks.Name
End If
Next
Sh(i + 1) = Sheets(32).Name
ReDim Preserve Sh(1 To i + 1)
Worksheets(Sh()).Select

End Sub
'---------------------------------------

Regards,
Jim Cone
San Francisco, USA



"Barney Byrd" wrote in message
...
The following code selects contiguous worksheets 5 through 11.
============
Sub SelectSheets()
Dim Sh() As Variant, wks As Worksheet, i As Integer
i = 0
For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2") = 1 Then
i = i + 1
ReDim Preserve Sh(1 To i)
Sh(i) = wks.Name
End If
Next
Worksheets(Sh()).Select

End Sub
===============
I would like to add one noncontiguous worksheet (#32) to the array of
selected worksheets. Is there a way to do so?
Barney Byrd


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Can I include noncontiguous worksheet in array of worksheets?

Thanks to both of you, Jim and OJ. With your help I was able to resolve my
problem.


Barney Byrd


"Jim Cone" wrote in message
...
Barney,

Something like this...
'---------------------------------
Sub SelectSheets()
Dim Sh() As String
Dim wks As Worksheet
Dim i As Long

ReDim Sh(1 To Sheets.Count)

For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2") = 1 Then
i = i + 1
Sh(i) = wks.Name
End If
Next
Sh(i + 1) = Sheets(32).Name
ReDim Preserve Sh(1 To i + 1)
Worksheets(Sh()).Select

End Sub
'---------------------------------------

Regards,
Jim Cone
San Francisco, USA



"Barney Byrd" wrote in message
...
The following code selects contiguous worksheets 5 through 11.
============
Sub SelectSheets()
Dim Sh() As Variant, wks As Worksheet, i As Integer
i = 0
For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2") = 1 Then
i = i + 1
ReDim Preserve Sh(1 To i)
Sh(i) = wks.Name
End If
Next
Worksheets(Sh()).Select

End Sub
===============
I would like to add one noncontiguous worksheet (#32) to the array of
selected worksheets. Is there a way to do so?
Barney Byrd




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Can I include noncontiguous worksheet in array of worksheets?

Or you can skip the option declaration and explicitly declare where you want
it to start like you were already doing. Obviously such choices represent
personal preference.

--
Regards,
Tom Ogilvy

"OJ" wrote in message
oups.com...
Hi, try this...
Worth noting the If you absolutely want the Array to start at 1 not 0
then move the line i=i+1 above the Redim Preserve Statement..

Sub SelectSheets()
Dim Sh() As Long, wks As Worksheet, i As Integer
For Each wks In ActiveWorkbook.Sheets
If wks.Range("E2").Value = 1 Or wks.Index = 32 Then
ReDim Preserve Sh(i) '''No Need for 1 to i.
'''If you want your arrays to start at 1 then you can use Option Base 1
'''in Declarations section of code
Sh(i) = wks.Index
i = i + 1
End If
Next
Worksheets(Sh()).Select
End Sub


Hth,
O



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I write an array to include all worksheets in a workbook? Jodie Excel Worksheet Functions 8 October 13th 09 04:06 PM
how do I include multiple worksheets to a countif formula? Brigette Excel Discussion (Misc queries) 1 August 19th 09 10:58 PM
include criteria to 'rank based array function' TUNGANA KURMA RAJU Excel Discussion (Misc queries) 2 September 2nd 06 01:15 PM
How to Include exl charts from closed worksheets into PPT slides akshetrapal Excel Programming 3 January 24th 04 06:13 PM
Is it possible to include Excel worksheets/workbooks into COM Addins ? alkan Excel Programming 1 November 14th 03 02:04 PM


All times are GMT +1. The time now is 12:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"