PC Review


Reply
Thread Tools Rate Thread

How can I check to see if a sheet exists?

 
 
Cruzian_Rain Girl
Guest
Posts: n/a
 
      14th Dec 2008
Hello.
I have to be able to write a code in Visual Basic that can check to see if a
sheet exists and post a msgbox saying 'true' or 'false', depending on the
answer.

Example- Check to see if 'Bob' exists.
if it does then Msgbox 'true'

Can anyone help me please?
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      14th Dec 2008
Sub sheetExist()
Dim ws As Worksheet
Dim foundSheet As Boolean
foundSheet = False
For Each ws In Worksheets
If ws.Name = "Sheet10" Then
foundSheet = True
End If
Next ws
MsgBox foundSheet
End Sub

"Cruzian_Rain Girl" wrote:

> Hello.
> I have to be able to write a code in Visual Basic that can check to see if a
> sheet exists and post a msgbox saying 'true' or 'false', depending on the
> answer.
>
> Example- Check to see if 'Bob' exists.
> if it does then Msgbox 'true'
>
> Can anyone help me please?

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      14th Dec 2008
You can write a generic procedure that will work for any worksheet in
any workbook.

Function SheetExists(SheetName As String, _
Optional WB As Workbook) As Boolean
Dim W As Workbook
If WB Is Nothing Then
Set W = ActiveWorkbook
Else
Set W = WB
End If
On Error Resume Next
SheetExists = CBool(Len(W.Worksheets(SheetName).Name))
End Function

Then, you can call it with code like



Dim B As Boolean
B = SheetExists("Sheet1")
If B = True Then
MsgBox "Sheet exists"
Else
MsgBox "Sheet does not exist"
End If

If you omit the WB parameter to SheetExists, the code tests the active
workbook for the presence of the worksheet. You can specify any open
workbook by including the WB parameter:

B = SheetExists("Sheet1",Workbooks("Test.xls"))

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Sun, 14 Dec 2008 11:29:01 -0800, Cruzian_Rain Girl <Cruzian_Rain
(E-Mail Removed)> wrote:

>Hello.
>I have to be able to write a code in Visual Basic that can check to see if a
>sheet exists and post a msgbox saying 'true' or 'false', depending on the
>answer.
>
>Example- Check to see if 'Bob' exists.
>if it does then Msgbox 'true'
>
>Can anyone help me please?

 
Reply With Quote
 
Lars-Åke Aspelin
Guest
Posts: n/a
 
      14th Dec 2008
On Sun, 14 Dec 2008 11:29:01 -0800, Cruzian_Rain Girl <Cruzian_Rain
(E-Mail Removed)> wrote:

>Hello.
>I have to be able to write a code in Visual Basic that can check to see if a
>sheet exists and post a msgbox saying 'true' or 'false', depending on the
>answer.
>
>Example- Check to see if 'Bob' exists.
>if it does then Msgbox 'true'
>
>Can anyone help me please?


Here is another way you may try;

Sub sheet_check()
On Error GoTo sheet_missing
MsgBox Worksheets("Bob").Name = "Bob"
Exit Sub
sheet_missing: MsgBox "false"
End Sub

Hope this helps / Lars-Åke
 
Reply With Quote
 
Frederik
Guest
Posts: n/a
 
      15th Dec 2008
Have a look at

http://spreadsheetpage.com/index.php...vba_functions/

Success!!!

--
met vriendelijke groetjes

(E-Mail Removed)
"Cruzian_Rain Girl" <Cruzian_Rain (E-Mail Removed)> schreef in
bericht news:886E1D22-661D-4252-8121-(E-Mail Removed)...
> Hello.
> I have to be able to write a code in Visual Basic that can check to see if
> a
> sheet exists and post a msgbox saying 'true' or 'false', depending on the
> answer.
>
> Example- Check to see if 'Bob' exists.
> if it does then Msgbox 'true'
>
> Can anyone help me please?



 
Reply With Quote
 
Cruzian_Rain Girl
Guest
Posts: n/a
 
      15th Dec 2008
Thank you so so much! The other codes helped a little, but this made it
really simple for me, thank you again!

"Frederik" wrote:

> Have a look at
>
> http://spreadsheetpage.com/index.php...vba_functions/
>
> Success!!!
>
> --
> met vriendelijke groetjes
>
> (E-Mail Removed)
> "Cruzian_Rain Girl" <Cruzian_Rain (E-Mail Removed)> schreef in
> bericht news:886E1D22-661D-4252-8121-(E-Mail Removed)...
> > Hello.
> > I have to be able to write a code in Visual Basic that can check to see if
> > a
> > sheet exists and post a msgbox saying 'true' or 'false', depending on the
> > answer.
> >
> > Example- Check to see if 'Bob' exists.
> > if it does then Msgbox 'true'
> >
> > Can anyone help me please?

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a sheet exists ? Luc Microsoft Excel Programming 4 28th Dec 2010 07:25 AM
How to check to see if a sheet with a particular name exists? Varun Microsoft Excel Worksheet Functions 3 25th Jan 2009 01:41 PM
check to see if sheet exists Wandering Mage Microsoft Excel Programming 1 28th Sep 2004 07:53 PM
Check if a sheet exists Jon Microsoft Excel Misc 4 6th May 2004 08:44 AM
check if sheet exists Ross Microsoft Excel Programming 3 25th Jul 2003 06:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:19 PM.