Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Looping Through Names

Dim varr as Variant
varr = Array("SheetA","Houses","Dogs")
for i = lbound(varr) to ubound(varr)
set sh = Worksheets(varr(i))
msgbox sh.Range("A1").Address(external:=true)
Next

--
Regards,
Tom Ogilvy

"The Wonder Thing" wrote in
message ...
Hello All.

Just wondering if there's a simpler way to do loop through a bunch of

names. Say the names of various worksheets. Right now I use:

Do While mySheetIndex <= 5
mySheetIndex = mySheetIndex +1
If mySheetIndex = 1 Then mySheet="Worksheet Name 1"
If mySheetIndex = 2 Then mySheet="Worksheet Name 2"
If mySheetIndex = 3 Then mySheet="Worksheet Name 3"
If mySheetIndex = 4 Then mySheet="Worksheet Name 4"
If mySheetIndex = 5 Then mySheet="Worksheet Name 5"
Worksheets(mySheet).Range("A1").Value = "This is currently sheet #: " &

mySheetIndex
Loop

I have to use the worksheet names, because they do get moved around a bit.

And of course I generally have a lot more code I want to execute per sheet
than just that one line. I was thinking something along the lines of an
array that contained all the names of the sheets I wanted to target (because
there are some I don't want to), and then I could just define the array up
at the begginning and do something like:

For each sht in ActiveWorkbook.Worksheets
If sht.Name Like [One of the values in mySheetArray] Then
Worksheets(sht).Range("A1").Value = "This sheet.... etc."
End If
Next

But I can't get it to work.
Anyways, thanks in advance for your help!



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
converting email address names in a range of cells to real names John Excel Worksheet Functions 1 May 19th 10 03:44 PM
using the Excel generic worksheet names instead of user-given names in code Paul Excel Discussion (Misc queries) 5 June 26th 09 08:44 PM
Sorting and matching rows of names with Socials with master list and eliminating the extra names Giacomo Excel Worksheet Functions 1 March 10th 07 01:52 AM
Can I check names in one list agains names in another in excel? John@Hospice of Hope Excel Discussion (Misc queries) 1 August 22nd 06 09:24 AM
Change names of files in a folder to match names in Excel Column saybut Excel Programming 4 February 9th 04 06:26 PM


All times are GMT +1. The time now is 10:47 PM.

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

About Us

"It's about Microsoft Excel"