Solved

Check for the existence of a sheet named 'Sheet1' in the current workbook

Posted on 2011-02-15
7
393 Views
Last Modified: 2012-06-21
Dear Experts:

I would like to check the following on the current workbook using VBA:

- Check whether  a sheet by the name 'Sheet1' exists. If not a msgbox is to say so!

Thank you very much in advance for your help.

Regards, Andreas
0
Comment
Question by:AndreasHermle
7 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 167 total points
ID: 34898469
DIm wks as worksheet
On error resume next
Set wks = Worksheets("SHeet1")
If err.number <> 0 then 
   msgbox "No sheet1!"
End If
On Error Goto 0

Open in new window


for example.
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 166 total points
ID: 34898496
Try this:
Sub FindSheet1()
    Dim objSheet As Worksheet
    Dim bSheetFound As Boolean
    
    bSheetFound = False
    For Each objSheet In Worksheets
        If objSheet.Name = "Sheet1" Then
            bSheetFound = True
            Exit For
        End If
    Next

    If Not bSheetFound Then
        MsgBox "Sheet1 not found!"
    End If
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:patrickab
ID: 34898804
Or try this:
Sub Sheetfinder()
    Dim ws As Worksheet
    Dim flag as Long

    For Each ws In Worksheets
        If ws.Name = "Sheet1" Then
          MsgBox "Sheet1 found"
          flag=1
        End If
    Next
    If flag = 1 Then
        MsgBox "Sheet1 not found"
    End If
End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Assisted Solution

by:patrickab
patrickab earned 167 total points
ID: 34899220
Oops - should have been:

Sub Sheetfinder()
    Dim ws As Worksheet
    Dim flag as Long

    For Each ws In Worksheets
        If ws.Name = "Sheet1" Then
          MsgBox "Sheet1 found"
          flag=1
        End If
    Next
    If flag = 0 Then
        MsgBox "Sheet1 not found"
    End If
End Sub
0
 

Author Comment

by:AndreasHermle
ID: 34904196
Dear all,

thank you very much for the overwhelming support. They all seem to work just fine. I will run a couple of more tests and then let you know.

Again, thank you very much for your professional support.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 34906383
Dear all,

since all three approaches work just fine, I would suggest splitting the points evenly.

Thank you again for your great help. Regards, Andreas
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34906400
Andreas - Thanks for the points - Patrick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to add the word OR to the front of each Excel value 4 37
sql server query from excel 3 57
Fixing a embedded format 7 29
Copy value from a certain cell 5 25
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

863 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

25 Experts available now in Live!

Get 1:1 Help Now