ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through specific worksheets (https://www.excelbanter.com/excel-programming/346690-loop-through-specific-worksheets.html)

Ben

Loop through specific worksheets
 
I am familiar with the code to loop through all the worksheets in a workbook
with a "For Each ws in Worksheets" construct but I would like a routine to be
confined to a selection of named worksheets within a workbook. Let's say the
worksheet names are in a range called "MyList" in a worksheet called
"Control". What code should I use to apply my routine only to the worksheets
in "Mylist".
Thank you.

Norman Jones

Loop through specific worksheets
 
Hi Ben,

Try something like:

'<<==============
Public Sub Tester02()
Dim SH As Worksheet
Dim rng As Range
Dim rCell As Range

Set rng = ActiveWorkbook.Sheets("Control").Range("MyList")

For Each SH In ActiveWorkbook.Worksheets
If Not IsError(Application.Match(SH.Name, rng, 0)) Then
'Do something, e.g.:
MsgBox SH.Name
End If
Next SH
End Sub
'==============


---
Regards,
Norman


"Ben" wrote in message
...
I am familiar with the code to loop through all the worksheets in a
workbook
with a "For Each ws in Worksheets" construct but I would like a routine to
be
confined to a selection of named worksheets within a workbook. Let's say
the
worksheet names are in a range called "MyList" in a worksheet called
"Control". What code should I use to apply my routine only to the
worksheets
in "Mylist".
Thank you.




Don Guillett[_4_]

Loop through specific worksheets
 
does this idea help? You do not usually have to specify the sheet name
unless you have duplicate names.

Sub loopthrusheetarray()
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Sheet2", "Sheet6", "Sheet10"))
MsgBox Sh.Range("a1")
Next
End Sub
--
Don Guillett
SalesAid Software

"Ben" wrote in message
...
I am familiar with the code to loop through all the worksheets in a
workbook
with a "For Each ws in Worksheets" construct but I would like a routine to
be
confined to a selection of named worksheets within a workbook. Let's say
the
worksheet names are in a range called "MyList" in a worksheet called
"Control". What code should I use to apply my routine only to the
worksheets
in "Mylist".
Thank you.




Ben

Loop through specific worksheets
 
Thank you to Don and Norman Both suggestions do the job.

"Don Guillett" wrote:

does this idea help? You do not usually have to specify the sheet name
unless you have duplicate names.

Sub loopthrusheetarray()
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Sheet2", "Sheet6", "Sheet10"))
MsgBox Sh.Range("a1")
Next
End Sub
--
Don Guillett
SalesAid Software

"Ben" wrote in message
...
I am familiar with the code to loop through all the worksheets in a
workbook
with a "For Each ws in Worksheets" construct but I would like a routine to
be
confined to a selection of named worksheets within a workbook. Let's say
the
worksheet names are in a range called "MyList" in a worksheet called
"Control". What code should I use to apply my routine only to the
worksheets
in "Mylist".
Thank you.





Don Guillett[_4_]

Loop through specific worksheets
 
glad to help

--
Don Guillett
SalesAid Software

"Ben" wrote in message
...
Thank you to Don and Norman Both suggestions do the job.

"Don Guillett" wrote:

does this idea help? You do not usually have to specify the sheet name
unless you have duplicate names.

Sub loopthrusheetarray()
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Sheet2", "Sheet6", "Sheet10"))
MsgBox Sh.Range("a1")
Next
End Sub
--
Don Guillett
SalesAid Software

"Ben" wrote in message
...
I am familiar with the code to loop through all the worksheets in a
workbook
with a "For Each ws in Worksheets" construct but I would like a routine
to
be
confined to a selection of named worksheets within a workbook. Let's
say
the
worksheet names are in a range called "MyList" in a worksheet called
"Control". What code should I use to apply my routine only to the
worksheets
in "Mylist".
Thank you.








All times are GMT +1. The time now is 11:30 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com