PC Review


Reply
Thread Tools Rate Thread

How to check the Sheet name exist before running Macro code

 
 
tlee
Guest
Posts: n/a
 
      5th Jan 2010
Hello all,

Could anyone know how to check the sheet name (e.g. "mySheet") exist before
running the rest macro code ?

Thanks,

tlee


 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      5th Jan 2010
Copy the below function and use that in code as below

Sub Macro()
'If sheet do not exist then exit procedure
If Not IsSheetExists("mySheet") Then Exit Sub

End Sub


Function IsSheetExists(strSheet As String) As Boolean
On Error Resume Next
Dim ws As Worksheet
Set ws = Sheets(strSheet)
If Not ws Is Nothing Then IsSheetExists = True
End Function

--
Jacob


"tlee" wrote:

> Hello all,
>
> Could anyone know how to check the sheet name (e.g. "mySheet") exist before
> running the rest macro code ?
>
> Thanks,
>
> tlee
>
>
> .
>

 
Reply With Quote
 
tlee
Guest
Posts: n/a
 
      5th Jan 2010
Hi Jacob,

Thank you very much for your help !

tlee

-----------------------------------------------------------------------------------------------------------
> Copy the below function and use that in code as below
>
> Sub Macro()
> 'If sheet do not exist then exit procedure
> If Not IsSheetExists("mySheet") Then Exit Sub
>
> End Sub
>
>
> Function IsSheetExists(strSheet As String) As Boolean
> On Error Resume Next
> Dim ws As Worksheet
> Set ws = Sheets(strSheet)
> If Not ws Is Nothing Then IsSheetExists = True
> End Function
>
> --
> Jacob
>
>
> "tlee" wrote:
>
>> Hello all,
>>
>> Could anyone know how to check the sheet name (e.g. "mySheet") exist
>> before
>> running the rest macro code ?
>>
>> Thanks,
>>
>> tlee
>>
>>
>> .
>>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      5th Jan 2010
Try a function like the following:

Function SheetExists(SheetName As String, Optional ByVal WB As
Workbook) As Boolean
If WB Is Nothing Then
Set WB = ThisWorkbook
End If
On Error Resume Next
SheetExists = CBool(Len(WB.Worksheets(SheetName).Name))
End Function


Set SheetName to the name of the worksheet to test, and set WB to the
workbook in which the sheet might exist. If you omit the WB parameter,
the code looks in the workbook that contains the code:

If SheetExists("Sheet5") = True Then
' do something
End If

' OR

If SheetExists("Sheet5",Workbooks("Book4.xls")) = True Then
' do something
End If

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




On Tue, 5 Jan 2010 12:53:44 +0800, "tlee" <(E-Mail Removed)> wrote:

>Hello all,
>
>Could anyone know how to check the sheet name (e.g. "mySheet") exist before
>running the rest macro code ?
>
>Thanks,
>
>tlee
>

 
Reply With Quote
 
tlee
Guest
Posts: n/a
 
      7th Jan 2010
Hello Chip,

Thank you so much for giving me alternative.

tlee

> Try a function like the following:
>
> Function SheetExists(SheetName As String, Optional ByVal WB As
> Workbook) As Boolean
> If WB Is Nothing Then
> Set WB = ThisWorkbook
> End If
> On Error Resume Next
> SheetExists = CBool(Len(WB.Worksheets(SheetName).Name))
> End Function
>
>
> Set SheetName to the name of the worksheet to test, and set WB to the
> workbook in which the sheet might exist. If you omit the WB parameter,
> the code looks in the workbook that contains the code:
>
> If SheetExists("Sheet5") = True Then
> ' do something
> End If
>
> ' OR
>
> If SheetExists("Sheet5",Workbooks("Book4.xls")) = True Then
> ' do something
> End If
>
> Cordially,
> Chip Pearson
> Microsoft MVP 1998 - 2010
> Pearson Software Consulting, LLC
> www.cpearson.com
> [email on web site]
>
>
>
>
> On Tue, 5 Jan 2010 12:53:44 +0800, "tlee" <(E-Mail Removed)> wrote:
>
>>Hello all,
>>
>>Could anyone know how to check the sheet name (e.g. "mySheet") exist
>>before
>>running the rest macro code ?
>>
>>Thanks,
>>
>>tlee
>>

 
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
Check if code already exist in a different table jeanulrich00@gmail.com Microsoft Access 1 22nd Oct 2008 05:16 PM
Help to code Macro to Copy fron one sheet and paste in other sheet kay Microsoft Excel Programming 3 25th Jul 2008 06:46 PM
Disable macro if sheet exist Charles Reid Microsoft Excel Discussion 1 26th Oct 2007 02:14 AM
How can I check in VB (Macro in Excel) if a file exist Alex Microsoft Excel Discussion 3 15th May 2006 09:57 AM
excel - macro code to open a protected sheet, enter passowrd, and then protect sheet arunjoshi Microsoft Excel Programming 1 2nd May 2004 03:50 PM


Features
 

Advertising
 

Newsgroups
 


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