Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default How to check the Sheet name exist before running Macro code

Hello all,

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

Thanks,

tlee


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default How to check the Sheet name exist before running Macro code

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


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default How to check the Sheet name exist before running Macro code

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


.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to check the Sheet name exist before running Macro code

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" wrote:

Hello all,

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

Thanks,

tlee

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default How to check the Sheet name exist before running Macro code

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" 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
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
Check Auto Filter - if on, turn off before running macro Munchkin Excel Programming 4 November 25th 09 03:43 AM
Want to check if cell is blank before running macro Munchkin Excel Programming 5 June 3rd 09 11:59 PM
Check values before running macro Dan R. Excel Programming 9 February 8th 07 04:54 PM
How to check a cell for content before running macro. Incoherent Excel Programming 1 October 7th 05 04:42 PM
Check cell for data before running code Pat Excel Programming 1 January 12th 05 08:58 PM


All times are GMT +1. The time now is 11:18 PM.

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"