Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Check whether a sheet is present in a workbook

Hello All,

I need some help in programming a macro.

I need to check whether a specific worksheet is present in a workbook.
If it is present, the macro follows one logic and if it does not it
follows another logic.

I tried referring to an cell value in that sheet,
If
ActiveWorkbook.Sheets("Alignment_Retail").Range("A 1").Value = "Select"
Then

But, while running the code, if the sheet "Alignment_Retail" is not
present in the workbook, VBA immediately throws an "subscript out of
range" error.

Can some one please help me to draw out a different logic. All I need
is to validate, whether the sheet is present in the workbook or not. I
cannot use the worksheet count logic as the number of sheets can vary.

Hoping to hear from you!!!

Thanks in advance,
Sagar
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Check whether a sheet is present in a workbook

Add this function
Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function

and then use it something like this...

if sheetexists("Alignment_Retail", activeworkbook) then
ActiveWorkbook.Sheets("Alignment_Retail").Range("A 1").Value = "Select"
end if
--
HTH...

Jim Thomlinson


"Sagu" wrote:

Hello All,

I need some help in programming a macro.

I need to check whether a specific worksheet is present in a workbook.
If it is present, the macro follows one logic and if it does not it
follows another logic.

I tried referring to an cell value in that sheet,
If
ActiveWorkbook.Sheets("Alignment_Retail").Range("A 1").Value = "Select"
Then

But, while running the code, if the sheet "Alignment_Retail" is not
present in the workbook, VBA immediately throws an "subscript out of
range" error.

Can some one please help me to draw out a different logic. All I need
is to validate, whether the sheet is present in the workbook or not. I
cannot use the worksheet count logic as the number of sheets can vary.

Hoping to hear from you!!!

Thanks in advance,
Sagar

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Check whether a sheet is present in a workbook

Here is one way:

Sub look()
On Error Resume Next
If Len(ThisWorkbook.Worksheets.Item("Alignment_Retail ").Name) = 0
Then
On Error GoTo 0
MsgBox "The sheet isn't there."
Else
MsgBox "The sheet is there."
End If
End Sub

I've done this before, but now use a sheet management procedure in all
my projects.

Cliff Edwards
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Check whether a sheet is present in a workbook

It worked out perfectly..

Thanks a million for you help, Cliff !!

Thanks,
Sagar


On Jun 9, 10:19*am, ward376 wrote:
Here is one way:

Sub look()
On Error Resume Next
* * If Len(ThisWorkbook.Worksheets.Item("Alignment_Retail ").Name) = 0
Then
On Error GoTo 0
* * * * MsgBox "The sheet isn't there."
* * Else
* * * * MsgBox "The sheet is there."
* * End If
End Sub

I've done this before, but now use a sheet management procedure in all
my projects.

Cliff Edwards


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
Locating if a Sheet is present in a workbook Shane Excel Discussion (Misc queries) 3 October 29th 09 08:20 PM
List all sheets present in Workbook Jeff Excel Worksheet Functions 6 November 9th 06 09:03 PM
Command for present sheet kanuvas[_9_] Excel Programming 2 November 14th 05 12:05 PM
Check if sheet exists in a closed workbook FrigidDigit[_2_] Excel Programming 2 October 25th 05 06:44 AM
How Do I Check To See Id A Character Is Present In A Cell Minitman[_4_] Excel Programming 4 May 23rd 04 08:09 PM


All times are GMT +1. The time now is 07:45 AM.

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"