Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JB JB is offline
external usenet poster
 
Posts: 115
Default How do I Test for the existence of a Worksheet from inside a Macro

I'm trying to run a macro, but sometimes theres a worksheet in place and
sometimes there isn't. If its not there I need to add it, but if it there
and I try to add it i get an error. Anyone know how to do an if statement
around something like this.

Thanks,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default How do I Test for the existence of a Worksheet from inside a Macro

Sub nosheet()
isthere = False
For Each w In Worksheets
If w.Name = "Sheetxx" Then
isthere = True
End If
Next

If isthere Then
MsgBox ("it's already there")
Else
MsgBox ("It's not there")
End If
End Sub

--
Gary''s Student
gsnu200711


"jb" wrote:

I'm trying to run a macro, but sometimes theres a worksheet in place and
sometimes there isn't. If its not there I need to add it, but if it there
and I try to add it i get an error. Anyone know how to do an if statement
around something like this.

Thanks,

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How do I Test for the existence of a Worksheet from inside a Macro

I stole this from Chip Pearson:

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

jb wrote:

I'm trying to run a macro, but sometimes theres a worksheet in place and
sometimes there isn't. If its not there I need to add it, but if it there
and I try to add it i get an error. Anyone know how to do an if statement
around something like this.

Thanks,


--

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
Test for the existence of a worksheet? Damien McBain[_3_] Excel Programming 5 March 19th 07 01:35 PM
Test for file existence Joseph[_46_] Excel Programming 1 November 12th 04 08:56 PM
Existence, Search, Test JohnNews Excel Programming 0 October 11th 04 05:48 PM
Test for existence of Name Jack Schitt Excel Programming 2 April 6th 04 06:51 PM
how do i test for the existence of a range name Jake Marx[_3_] Excel Programming 2 March 3rd 04 09:57 PM


All times are GMT +1. The time now is 05:04 PM.

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"