PC Review


Reply
Thread Tools Rate Thread

Check whether a sheet is present in a workbook

 
 
Sagu
Guest
Posts: n/a
 
      9th Jun 2008
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("A1").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
 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      9th Jun 2008
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("A1").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("A1").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
>

 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      9th Jun 2008
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 With Quote
 
Sagu
Guest
Posts: n/a
 
      9th Jun 2008
It worked out perfectly..

Thanks a million for you help, Cliff !!

Thanks,
Sagar


On Jun 9, 10:19*am, ward376 <cledwards1...@gmail.com> 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 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
Save each sheet of multi-sheet workbook as a single-sheet workbook CTB Microsoft Excel Programming 3 17th May 2011 08:40 AM
How to check if an Add In is present. rick Microsoft Excel Programming 7 30th Nov 2009 03:52 PM
Locating if a Sheet is present in a workbook Shane Microsoft Excel Misc 3 29th Oct 2009 08:20 PM
List all sheets present in Workbook =?Utf-8?B?SkVGRg==?= Microsoft Excel Worksheet Functions 6 9th Nov 2006 09:03 PM
Check if sheet exists in a closed workbook FrigidDigit Microsoft Excel Programming 2 25th Oct 2005 06:44 AM


Features
 

Advertising
 

Newsgroups
 


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