Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Cycling through worksheets

Hi,

I'm cycling through the worksheets of a workbook
extracting their name. I'm using the following code which
is "clunky" - what's a better way of doing it..??

For Each sheet In ActiveWorkbook.Worksheets

sheet.Activate
my var = ActiveSheet.Name

Next sheet

thks

Chris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Cycling through worksheets

Hi Chris,

When you loop through the Worksheets collection, the variable you use in the
For Each statement will hold a reference to the current Worksheet in the
collection. So you should use that instead of ActiveSheet:

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
MsgBox ws.Name
Next ws

Alternatively, you could get all the Worksheet names into an array if you'd
like:

Dim asWSNames(1 to 1) As String
Dim nSheetCount As Integer
Dim nSheetNo As Integer

nSheetCount = ActiveWorkbook.Worksheets.Count
Redim WSNames(1 to nSheetCount)

For nSheetNo = 1 To nSheetCount
WSNames(nSheetNo) = ActiveWorkbook.Worksheets(nSheetNo).Name
Next nSheetNo

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Chris Gorham wrote:
Hi,

I'm cycling through the worksheets of a workbook
extracting their name. I'm using the following code which
is "clunky" - what's a better way of doing it..??

For Each sheet In ActiveWorkbook.Worksheets

sheet.Activate
my var = ActiveSheet.Name

Next sheet

thks

Chris


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Cycling through worksheets

With a macro you can do this

It will add a sheet to your workbook and add the sheetnames

Sub TabNames()
Application.ScreenUpdating = False
Dim Nsheet As Worksheet
Set Nsheet = Sheets.Add
Dim WS As Worksheet
Dim r As Integer
r = 1
For Each WS In ThisWorkbook.Worksheets
If WS.Name < Nsheet.Name Then
Nsheet.Range("A" & r) = WS.Name
r = r + 1
End If
Next WS
Application.ScreenUpdating = True
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Chris Gorham" wrote in message ...
Hi,

I'm cycling through the worksheets of a workbook
extracting their name. I'm using the following code which
is "clunky" - what's a better way of doing it..??

For Each sheet In ActiveWorkbook.Worksheets

sheet.Activate
my var = ActiveSheet.Name

Next sheet

thks

Chris



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
Tab keeps cycling through the same row Mike Excel Discussion (Misc queries) 2 August 14th 08 12:20 AM
Cycling through workbooks Nico Excel Discussion (Misc queries) 4 July 4th 08 10:09 PM
Anyone out there knows of running cycling and swimming logs? Tlaloc Excel Discussion (Misc queries) 4 January 21st 05 04:29 AM
Open a Read-Only File by cycling thru these 5 passwords J.E. McGimpsey Excel Programming 1 August 30th 03 03:33 AM
Need UDF help; cycling through various sheets using VBA name property Keith R[_3_] Excel Programming 3 July 23rd 03 02:06 PM


All times are GMT +1. The time now is 09:20 AM.

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"