ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to check from VBA if sheet exists? (https://www.excelbanter.com/excel-programming/354852-how-check-vba-if-sheet-exists.html)

Alen

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

Thierry Paradis

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




Dave Peterson

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


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com