Solved

Retrieve the names of worksheets and display them in a MsgBox

Posted on 2013-12-15
5
166 Views
Last Modified: 2013-12-17
Dear Experts:

On the currently open workbook I would like to perform the following action using VBA:

Retrieve the worksheets' names of all the worksheets of the currently open workbook with  the exception of the sheets named 'Main', 'Calculation' and any sheets with the default names such as 'sheet1'.

The worksheet names are to be displayed in a MsgBox. I know that there is a character limitation on MsgBoxes.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
Martin Liss earned 350 total points
ID: 39720394
Dim ws As Worksheet
dim strNames as String

For Each ws In worksheets
    If Left(ws.Name,5) = "Sheet" or ws.Name = "Main" or ws.Name = "Calculation" Then
        ' do nothing
    Else
        strNames = strNames & ws.Name & " "
    End If
Next

MsgBox strNames

Open in new window

0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 150 total points
ID: 39720402
Sub listsheets()
    Dim ws As Worksheet
    Dim wslist As String
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> ws.CodeName And ws.Name <> "Main" And ws.Name <> "Calculation" Then
            wslist = wslist & IIf(wslist = "", "", vbCrLf) & ws.Name
        End If
    Next ws
    MsgBox wslist
End Sub
0
 

Author Comment

by:AndreasHermle
ID: 39720466
Hi ssaqibh,

the sheet names are nicely listed in your msgbox although I am afraid to tell you that the msgbox still lists 'Sheet1'. ws.CodeName does not seem to work.

Martin: Great job. Works just fine. Thank you very much.


Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39723368
Dear both,

great job from both of you. By combining the codes I am getting exactly the result I was looking for.

Regards, Andreas
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39724485
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now