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 to check from VBA if sheet exists?

How to check from VBA if sheet named "JohnDoe" exists in workbook?

Sub del_sheet()

if exist(sheets("JohnDoe")) then
application.displayalerts = false
sheets("JohnDoe").delete
application.displayalerts = true
end if

With what code I have to put instead of "exist(sheets("JohnDoe"))"

Regards, Alen
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default How to check from VBA if sheet exists?

Here's the code:

Public Sub VérifierFeuille(ByVal strValeur As String)
Dim wSheet As Worksheet

On Error Resume Next
Set wSheet = ActiveWorkbook.Sheets(strValeur)
If wSheet Is Nothing Then
MsgBox "The sheet" + strValeur + " is not present, the application
will be stop.", vbCritical + vbOKOnly, "Error"
End
Else
Set wSheet = Nothing
End If
End Sub


"Alen" a écrit dans le message de news:
...
How to check from VBA if sheet named "JohnDoe" exists in workbook?

Sub del_sheet()

if exist(sheets("JohnDoe")) then
application.displayalerts = false
sheets("JohnDoe").delete
application.displayalerts = true
end if

With what code I have to put instead of "exist(sheets("JohnDoe"))"

Regards, Alen



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to check from VBA if sheet exists?

Chip Pearson posted this function:

Function WorksheetExists(SheetName As Variant, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function

'and you can use it like:
....
if worksheetexists("myname",activeworkbook) then
application.displayalerts = false
worksheets("Myname").delete
application.displayalerts = true
end if

============
But if I'm deleting, I don't ca

application.displayalerts = false
on error resume next
sheets("JohnDoe").delete
on error goto 0
application.displayalerts = true

If it's not there, just ignore the error.

Alen wrote:

How to check from VBA if sheet named "JohnDoe" exists in workbook?

Sub del_sheet()

if exist(sheets("JohnDoe")) then
application.displayalerts = false
sheets("JohnDoe").delete
application.displayalerts = true
end if

With what code I have to put instead of "exist(sheets("JohnDoe"))"

Regards, Alen


--

Dave Peterson
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
check to see if sheet exists Wandering Mage Excel Programming 1 September 28th 04 07:53 PM
Check to see if sheet exists Steph[_3_] Excel Programming 4 September 22nd 04 12:47 AM
check if sheet exists Ross[_6_] Excel Programming 3 July 25th 03 06:46 PM


All times are GMT +1. The time now is 04:24 AM.

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

About Us

"It's about Microsoft Excel"