Excel VBA loop to make an array of wokrsheet names

Hi,
I'm trying to make an array that is a list of all the worksheet names in my excel document, this script seems close but doesn't quite work, instead it lists a seriies of references to various cells, can you please help?

Sub Worksheet_define()

Dim WSheets() As String
Dim iCount As Integer
Dim Max As Integer
    Max = ThisWorkbook.Sheets.Count
    ReDim WSheets(1 To Max)
    For iCount = 1 To Max
        WSheets(iCount) = ThisWorkbook.Names(iCount)
    Next iCount

End Sub

Thanks, WTC
WTC_ServicesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dlmilleCommented:
Try

WSheets(iCount) = ThisWorkbook.Names(iCount).Name
0
mikeopoloCommented:
Try:

Sub Worksheet_define()

Dim WSheets() As Variant
Dim iCount As Integer
Dim Max As Integer
    Max = ThisWorkbook.Worksheets.count
    ReDim WSheets(1 To Max)
    For iCount = 1 To Max
        WSheets(iCount) = ThisWorkbook.Worksheets(iCount).Name
    Next iCount

End Sub
0
CubicleGuyCommented:
Try changing

ThisWorkbook.Names(iCount)

to

Sheets(iCount).name
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dlmilleCommented:
Actually you want:

wSheets(iCount) = Thisworkbook.Worksheets(iCount).name.

Here's the way I do it, a bit differently, however:

Sub Worksheet_define()
Dim WSheets() As String
Dim iCount As Integer
Dim Max As Integer
Dim wks As Worksheet

    For Each wks In ThisWorkbook.Worksheets
        ReDim Preserve WSheets(iCount) As String
        WSheets(iCount) = wks.Name
        iCount = iCount + 1
    Next wks
   

End Sub
0
AgeOfEmpiresCommented:
And yet another alternative  

Using a dictionary object  (add reference to the Microsoft Scripting Runtime via Tools/References)

Sub Worksheet_define()
  Dim ws as Worksheet
  Dim d as New Dictionary

  For Each ws in ThisWorkbook.Worksheets
    d(ws.Name) = ""
  Next ws

End Sub

0
dlmilleCommented:
Very strange that you did not choose mikeopolo's suggested post.  It appears exactly correct, and was posted earlier than the one you selected.

In addition, using Sheets(iCount).name does not appear 100% correct, if your Excel workbook contains chart tabs - they would not be included in your array of names.  Also, if the ActiveWorkbook were not ThisWorkbook, you'd get the wrong list of sheet names :)

Cheers,

Dave
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.