Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieve the names of worksheets and display them in a MsgBox

Posted on 2013-12-15
5
Medium Priority
?
211 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 49

Accepted Solution

by:
Martin Liss earned 1400 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 600 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 49

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

916 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