PC Review


Reply
Thread Tools Rate Thread

How do I Test for the existence of a Worksheet from inside a Macro

 
 
=?Utf-8?B?amI=?=
Guest
Posts: n/a
 
      21st Mar 2007
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,
 
Reply With Quote
 
 
 
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      21st Mar 2007
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,

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      21st Mar 2007
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 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
Macro: how to create label forms inside the worksheet not userform HammerJoe@gmail.com Microsoft Excel Programming 2 26th Jun 2008 07:37 AM
Test for the existence of a worksheet? Damien McBain Microsoft Excel Programming 5 19th Mar 2007 01:35 PM
Existence, Search, Test JohnNews Microsoft Excel New Users 1 12th Oct 2004 01:05 PM
Macro to save as using two cell values inside worksheet sdblack Microsoft Excel Programming 1 24th Sep 2004 03:21 PM
Test for existence of Name Jack Schitt Microsoft Excel Programming 2 6th Apr 2004 06:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:28 PM.