Results 1 to 6 of 6

Thread: [RESOLVED] Is the sheet exist???

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Resolved [RESOLVED] Is the sheet exist???

    Hi Guys,

    I'm looking for a macro or built-in fonction that can tell me, by the sheet name, if a sheet already exist in a workbook.

    Excel 2003, VBA macro.

    Thanks

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Is the sheet exist???

    VB Code:
    1. Function SheetExists(ByVal BookName As String, ByVal SheetName As String) As Boolean
    2. Dim wksSheet As Worksheet
    3. Dim bTemp As Boolean
    4.  
    5.     For Each wksSheet In Workbooks(BookName).Worksheets
    6.         If wksSheet.Name = SheetName Then
    7.               bTemp = True
    8.               Exit For
    9.         End If
    10.     Next wksSheet
    11.    
    12.     SheetExists = bTemp
    13.    
    14. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Is the sheet exist???

    I coudn't ask for better


    thanks a lot

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Is the sheet exist???

    I coudn't ask for better
    Good, because I couldn't have done much better.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Is the sheet exist???

    Good, because I couldn't have done much better.
    I lied...
    You could also check to see of the workbook exists first...
    VB Code:
    1. Function SheetExists(ByVal BookName As String, ByVal SheetName As String) As Boolean
    2. Dim wkbBook As Workbook
    3. Dim wksSheet As Worksheet
    4. Dim bTemp As Boolean
    5.    
    6.     For Each wkbBook In Application.Workbooks
    7.        
    8.         If wkbBook.Name = BookName Then
    9.            
    10.             For Each wksSheet In wkbBook.Worksheets
    11.                
    12.                 If wksSheet.Name = SheetName Then
    13.                       bTemp = True
    14.                       Exit For
    15.                 End If
    16.                
    17.             Next wksSheet
    18.            
    19.         End If
    20.        
    21.         If bTemp Then Exit For
    22.     Next wkbBook
    23.    
    24.     SheetExists = bTemp
    25.    
    26. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: [RESOLVED] Is the sheet exist???

    Thanks...

    but just before I check if the sheet exists I'm opening the workbook.
    So I would have trapped the error before

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width