PC Review


Reply
Thread Tools Rate Thread

How do I assign large number of named worksheets to a varArray

 
 
Nabil
Guest
Posts: n/a
 
      28th Apr 2008
how can I assign a large number of named sheets to an array variable
(varArray).
In otherwords if I have a collection of large number of named worksheets
like "ASD","XZC","BNM",.......etc.
I want to assign them to the array variable varArray, like;
varArray = "ASD","XZC",......
Then I want to use it in the following statement :
For each asheet in activeworkbook.sheets(varArray)
Please notice that :
-I want to limit the search to limited number of sheets of the
activeworkbook, but the number of these sheets are relatively large ( more
than 20 sheets).
-I want to assign the sheets to the varArray in one seperate line, so that I
can increase r decrease the number of sheets anytime
-The activeworkbook contains many sheets, but I want to limit the search (
Done by For ...each )to a certain number out of the total sheet number.
-Are there a limit to the number of sheets used with the statement for...
each?
- I am using Excel 2003 and Excel 2007.
Thank you
--
Nabil A Youssef
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      28th Apr 2008
The following will create an array and assign sheet names to it:

Sub sheetnames()
Dim ary() As String
ReDim ary(1 To Sheets.Count)
For i = 1 To UBound(ary)
ary(i) = Sheets(i).Name
MsgBox (ary(i))
Next
End Sub

But you do not need the sheet names to refer to, or loop over, sheets; just
use the index.
--
Gary''s Student - gsnu200781


"Nabil" wrote:

> how can I assign a large number of named sheets to an array variable
> (varArray).
> In otherwords if I have a collection of large number of named worksheets
> like "ASD","XZC","BNM",.......etc.
> I want to assign them to the array variable varArray, like;
> varArray = "ASD","XZC",......
> Then I want to use it in the following statement :
> For each asheet in activeworkbook.sheets(varArray)
> Please notice that :
> -I want to limit the search to limited number of sheets of the
> activeworkbook, but the number of these sheets are relatively large ( more
> than 20 sheets).
> -I want to assign the sheets to the varArray in one seperate line, so that I
> can increase r decrease the number of sheets anytime
> -The activeworkbook contains many sheets, but I want to limit the search (
> Done by For ...each )to a certain number out of the total sheet number.
> -Are there a limit to the number of sheets used with the statement for...
> each?
> - I am using Excel 2003 and Excel 2007.
> Thank you
> --
> Nabil A Youssef

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Apr 2008
A structure like this should do what you want; just put your active code
between the With/End With statements using a leading dot for sheet items so
that item will refer to the currently referred to sheet, like my
..Cells(1,1).Value example....

Dim SH As Variant
Dim MySheets As Variant
MySheets = Array("Sheet1", "Sheet4", "Sheet7")
For Each SH In MySheets
With Sheets(SH)
Debug.Print .Cells(1, 1).Value
End With
Next

Rick


"Nabil" <(E-Mail Removed)> wrote in message
news:89ABE257-1DBC-4B6C-ACE5-(E-Mail Removed)...
> how can I assign a large number of named sheets to an array variable
> (varArray).
> In otherwords if I have a collection of large number of named worksheets
> like "ASD","XZC","BNM",.......etc.
> I want to assign them to the array variable varArray, like;
> varArray = "ASD","XZC",......
> Then I want to use it in the following statement :
> For each asheet in activeworkbook.sheets(varArray)
> Please notice that :
> -I want to limit the search to limited number of sheets of the
> activeworkbook, but the number of these sheets are relatively large ( more
> than 20 sheets).
> -I want to assign the sheets to the varArray in one seperate line, so that
> I
> can increase r decrease the number of sheets anytime
> -The activeworkbook contains many sheets, but I want to limit the search (
> Done by For ...each )to a certain number out of the total sheet number.
> -Are there a limit to the number of sheets used with the statement for...
> each?
> - I am using Excel 2003 and Excel 2007.
> Thank you
> --
> Nabil A Youssef


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Apr 2008
So there is no confusion with 'shape' of the example names I used, the Array
function assignment statement for example sheet names you posted would be...

MySheets = Array("ASD", "XZC", "BNM")

Just add or remove your sheet names in this statement as needed.

Rick



"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
>A structure like this should do what you want; just put your active code
>between the With/End With statements using a leading dot for sheet items so
>that item will refer to the currently referred to sheet, like my
>.Cells(1,1).Value example....
>
> Dim SH As Variant
> Dim MySheets As Variant
> MySheets = Array("Sheet1", "Sheet4", "Sheet7")
> For Each SH In MySheets
> With Sheets(SH)
> Debug.Print .Cells(1, 1).Value
> End With
> Next
>
> Rick
>
>
> "Nabil" <(E-Mail Removed)> wrote in message
> news:89ABE257-1DBC-4B6C-ACE5-(E-Mail Removed)...
>> how can I assign a large number of named sheets to an array variable
>> (varArray).
>> In otherwords if I have a collection of large number of named worksheets
>> like "ASD","XZC","BNM",.......etc.
>> I want to assign them to the array variable varArray, like;
>> varArray = "ASD","XZC",......
>> Then I want to use it in the following statement :
>> For each asheet in activeworkbook.sheets(varArray)
>> Please notice that :
>> -I want to limit the search to limited number of sheets of the
>> activeworkbook, but the number of these sheets are relatively large (
>> more
>> than 20 sheets).
>> -I want to assign the sheets to the varArray in one seperate line, so
>> that I
>> can increase r decrease the number of sheets anytime
>> -The activeworkbook contains many sheets, but I want to limit the search
>> (
>> Done by For ...each )to a certain number out of the total sheet number.
>> -Are there a limit to the number of sheets used with the statement for...
>> each?
>> - I am using Excel 2003 and Excel 2007.
>> Thank you
>> --
>> Nabil A Youssef

>


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Apr 2008
It just occurred to me, you might be wanting to change (add or remove)
sheets dynamically in code. The Array function does not really lend itself
to doing that. Examine this code snippet instead to see a method of being
able to actively change the number of referred to sheets...

Dim SH As Variant
Dim MySheets As Variant
Dim SheetNames As String
' Hard coded list of names
MySheets = Split("ASD,XZC,BNM", ",")
For Each SH In MySheets
With Sheets(SH)
Debug.Print .Cells(1, 1).Value
End With
Next
' Dynamically assigned list of names
SheetNames = "ASD,XZC,BNM,XXX,YYY,ZZZ"
MySheets = Split(SheetNames, ",")
For Each SH In MySheets
With Sheets(SH)
Debug.Print .Cells(1, 1).Value
End With
Next

Notice the list of names in the first argument of the Split function is a
single, comma delimited String (no internal quote marks).

Rick


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
>A structure like this should do what you want; just put your active code
>between the With/End With statements using a leading dot for sheet items so
>that item will refer to the currently referred to sheet, like my
>.Cells(1,1).Value example....
>
> Dim SH As Variant
> Dim MySheets As Variant
> MySheets = Array("Sheet1", "Sheet4", "Sheet7")
> For Each SH In MySheets
> With Sheets(SH)
> Debug.Print .Cells(1, 1).Value
> End With
> Next
>
> Rick
>
>
> "Nabil" <(E-Mail Removed)> wrote in message
> news:89ABE257-1DBC-4B6C-ACE5-(E-Mail Removed)...
>> how can I assign a large number of named sheets to an array variable
>> (varArray).
>> In otherwords if I have a collection of large number of named worksheets
>> like "ASD","XZC","BNM",.......etc.
>> I want to assign them to the array variable varArray, like;
>> varArray = "ASD","XZC",......
>> Then I want to use it in the following statement :
>> For each asheet in activeworkbook.sheets(varArray)
>> Please notice that :
>> -I want to limit the search to limited number of sheets of the
>> activeworkbook, but the number of these sheets are relatively large (
>> more
>> than 20 sheets).
>> -I want to assign the sheets to the varArray in one seperate line, so
>> that I
>> can increase r decrease the number of sheets anytime
>> -The activeworkbook contains many sheets, but I want to limit the search
>> (
>> Done by For ...each )to a certain number out of the total sheet number.
>> -Are there a limit to the number of sheets used with the statement for...
>> each?
>> - I am using Excel 2003 and Excel 2007.
>> Thank you
>> --
>> Nabil A Youssef

>


 
Reply With Quote
 
Nabil
Guest
Posts: n/a
 
      29th Apr 2008
Thank you for your help , your code could answer my question. It seems that
you are a proffesional in vba.
Good luck
--
Nabil A Youssef


"Rick Rothstein (MVP - VB)" wrote:

> A structure like this should do what you want; just put your active code
> between the With/End With statements using a leading dot for sheet items so
> that item will refer to the currently referred to sheet, like my
> ..Cells(1,1).Value example....
>
> Dim SH As Variant
> Dim MySheets As Variant
> MySheets = Array("Sheet1", "Sheet4", "Sheet7")
> For Each SH In MySheets
> With Sheets(SH)
> Debug.Print .Cells(1, 1).Value
> End With
> Next
>
> Rick
>
>
> "Nabil" <(E-Mail Removed)> wrote in message
> news:89ABE257-1DBC-4B6C-ACE5-(E-Mail Removed)...
> > how can I assign a large number of named sheets to an array variable
> > (varArray).
> > In otherwords if I have a collection of large number of named worksheets
> > like "ASD","XZC","BNM",.......etc.
> > I want to assign them to the array variable varArray, like;
> > varArray = "ASD","XZC",......
> > Then I want to use it in the following statement :
> > For each asheet in activeworkbook.sheets(varArray)
> > Please notice that :
> > -I want to limit the search to limited number of sheets of the
> > activeworkbook, but the number of these sheets are relatively large ( more
> > than 20 sheets).
> > -I want to assign the sheets to the varArray in one seperate line, so that
> > I
> > can increase r decrease the number of sheets anytime
> > -The activeworkbook contains many sheets, but I want to limit the search (
> > Done by For ...each )to a certain number out of the total sheet number.
> > -Are there a limit to the number of sheets used with the statement for...
> > each?
> > - I am using Excel 2003 and Excel 2007.
> > Thank you
> > --
> > Nabil A Youssef

>
>

 
Reply With Quote
 
Nabil
Guest
Posts: n/a
 
      29th Apr 2008
this code also replied my question
thank you
--
Nabil A Youssef


"Rick Rothstein (MVP - VB)" wrote:

> It just occurred to me, you might be wanting to change (add or remove)
> sheets dynamically in code. The Array function does not really lend itself
> to doing that. Examine this code snippet instead to see a method of being
> able to actively change the number of referred to sheets...
>
> Dim SH As Variant
> Dim MySheets As Variant
> Dim SheetNames As String
> ' Hard coded list of names
> MySheets = Split("ASD,XZC,BNM", ",")
> For Each SH In MySheets
> With Sheets(SH)
> Debug.Print .Cells(1, 1).Value
> End With
> Next
> ' Dynamically assigned list of names
> SheetNames = "ASD,XZC,BNM,XXX,YYY,ZZZ"
> MySheets = Split(SheetNames, ",")
> For Each SH In MySheets
> With Sheets(SH)
> Debug.Print .Cells(1, 1).Value
> End With
> Next
>
> Notice the list of names in the first argument of the Split function is a
> single, comma delimited String (no internal quote marks).
>
> Rick
>
>
> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
> message news:%(E-Mail Removed)...
> >A structure like this should do what you want; just put your active code
> >between the With/End With statements using a leading dot for sheet items so
> >that item will refer to the currently referred to sheet, like my
> >.Cells(1,1).Value example....
> >
> > Dim SH As Variant
> > Dim MySheets As Variant
> > MySheets = Array("Sheet1", "Sheet4", "Sheet7")
> > For Each SH In MySheets
> > With Sheets(SH)
> > Debug.Print .Cells(1, 1).Value
> > End With
> > Next
> >
> > Rick
> >
> >
> > "Nabil" <(E-Mail Removed)> wrote in message
> > news:89ABE257-1DBC-4B6C-ACE5-(E-Mail Removed)...
> >> how can I assign a large number of named sheets to an array variable
> >> (varArray).
> >> In otherwords if I have a collection of large number of named worksheets
> >> like "ASD","XZC","BNM",.......etc.
> >> I want to assign them to the array variable varArray, like;
> >> varArray = "ASD","XZC",......
> >> Then I want to use it in the following statement :
> >> For each asheet in activeworkbook.sheets(varArray)
> >> Please notice that :
> >> -I want to limit the search to limited number of sheets of the
> >> activeworkbook, but the number of these sheets are relatively large (
> >> more
> >> than 20 sheets).
> >> -I want to assign the sheets to the varArray in one seperate line, so
> >> that I
> >> can increase r decrease the number of sheets anytime
> >> -The activeworkbook contains many sheets, but I want to limit the search
> >> (
> >> Done by For ...each )to a certain number out of the total sheet number.
> >> -Are there a limit to the number of sheets used with the statement for...
> >> each?
> >> - I am using Excel 2003 and Excel 2007.
> >> Thank you
> >> --
> >> Nabil A Youssef

> >

>
>

 
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
Rapidly Create a Large Number of Named Ranges Thomas M. Microsoft Excel Worksheet Functions 0 7th May 2009 07:09 PM
sum a large number of worksheets Michael Microsoft Excel Misc 5 21st Jan 2009 05:33 PM
How do I assign a LARGE number of business contacts to an account =?Utf-8?B?QUVC?= Microsoft Outlook BCM 7 20th Sep 2007 04:38 AM
How do i assign the ActiveWorkbook.Worksheets to a worksheets object? TS Microsoft Excel Programming 2 27th Dec 2006 02:49 PM
Recalculation in a workbook with a large number of worksheets =?Utf-8?B?S2luc2xleQ==?= Microsoft Excel Discussion 1 1st Mar 2006 06:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:43 AM.