Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2005, 09:51 AM
Authorized User
 
Join Date: Sep 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default finding a worksheet

Hello! Firstly thanks everyone for their help so far with my many questions. I really appreciate it and I am learning loads as a result.

i have a slightly more complex question i think...

i need to know if a worksheet exists in a given workbook. Over time, worksheets have been added, and removed, the result being that i have something like Sheet1, Sheet2, Sheet5, Sheet9 etc.

is worksheets(3) the same as worksheets("Sheet5") in the above example? If not, how can I loop through each to find out if the required worksheet exists?

Thanks

Patrick

Visit my site: http://www.drybonesuk.com
__________________
Visit my site: http://www.drybonesuk.com
 
Old November 16th, 2005, 10:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Patric,

Worksheets(3) uses index number and in your case Sheet5 is third sheet in worksheet collection.
If you move Sheet5 to another location in workbook, it´s not anymore Worksheets(3).

Here is one example how to loop all sheets

Sub LoopWorkSheets()
Dim ws As Worksheet

For Each ws In Worksheets
    If ws.Name = "Sheet5" Then
        'Do something
    End If
Next
End Sub

-vemaju

 
Old November 16th, 2005, 10:40 AM
Authorized User
 
Join Date: Oct 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered
Default

cycle through the existing sheets checking for the name you want - in this example I'm simply passing the name of the sheet I want and the function returns true if it exists and false if it doesn't..


Function checkSheet(mySheet As String) As Boolean
bFound = False
For Each Sheet In Worksheets
    If Sheet.Name = mySheet Then bFound = True
Next
checkSheet = bFound
End Function

 
Old November 16th, 2005, 10:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Worksheets(3) is same as Worksheets("Sheet5") in the above example.

Regards
Ankur Verma
 
Old November 16th, 2005, 10:54 AM
Authorized User
 
Join Date: Sep 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by shattered
 cycle through the existing sheets checking for the name you want - in this example I'm simply passing the name of the sheet I want and the function returns true if it exists and false if it doesn't..


Function checkSheet(mySheet As String) As Boolean
bFound = False
For Each Sheet In Worksheets
    If Sheet.Name = mySheet Then bFound = True
Next
checkSheet = bFound
End Function

fantastic. thanks yet again for your replies. I have used the above example and it has worked fine. Thanks so much.

Patrick

Visit my site: http://www.drybonesuk.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Open up Worksheet alannoble26 Excel VBA 5 February 13th, 2006 02:24 PM
Email Worksheet alannoble26 Excel VBA 2 November 7th, 2005 10:33 AM
Send Worksheet alannoble26 Excel VBA 3 November 2nd, 2005 01:04 PM
Setting Worksheet name marcusfromsweden XSLT 0 September 19th, 2005 11:50 AM
how to name worksheet by Month yylee Excel VBA 2 April 10th, 2004 01:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.