Solved

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

Posted on 2011-02-15
7
392 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

760 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

21 Experts available now in Live!

Get 1:1 Help Now