Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How can I check to see if a sheet exists?

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default How can I check to see if a sheet exists?

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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How can I check to see if a sheet exists?

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
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?

  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How can I check to see if a sheet exists?

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


"Cruzian_Rain Girl" <Cruzian_Rain
schreef in
bericht ...
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check to see if a sheet with a particular name exists? Varun Excel Worksheet Functions 3 January 25th 09 01:41 PM
check if sheet exists mohavv Excel Discussion (Misc queries) 1 November 21st 07 01:58 AM
How to check from VBA if sheet exists? Alen Excel Programming 2 March 2nd 06 12:36 PM
check to see if sheet exists Wandering Mage Excel Programming 1 September 28th 04 07:53 PM
check if sheet exists Ross[_6_] Excel Programming 3 July 25th 03 06:46 PM


All times are GMT +1. The time now is 03:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"