Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 16th, 2005, 09:51 AM
Authorized User
 
Join Date: Sep 2003
Location: , , United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old November 16th, 2005, 10:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: , , Finland.
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

Reply With Quote
  #3 (permalink)  
Old November 16th, 2005, 10:40 AM
Authorized User
 
Join Date: Oct 2004
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old November 16th, 2005, 10:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
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
Reply With Quote
  #5 (permalink)  
Old November 16th, 2005, 10:54 AM
Authorized User
 
Join Date: Sep 2003
Location: , , United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:26 AM.


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