?
Solved

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

Posted on 2011-02-15
7
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 668 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 664 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Assisted Solution

by:patrickab
patrickab earned 668 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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