+ Reply to Thread
Results 1 to 5 of 5

User Defined Function to check whether sheet name exists is not working

  1. #1
    Registered User
    Join Date
    09-24-2019
    Location
    Chennai, India
    MS-Off Ver
    16.16.13
    Posts
    1

    User Defined Function to check whether sheet name exists is not working

    Hi Team,

    Please highlight what is the mistake in the code. It is able to return the value "Sheet already exists" when the sheet name exists. In else condition it throws #VALUE! error.

    Function SundarSheetExists(SheetName As String) As String
    For Each Sheet In Worksheets
    If SheetName = Worksheets(SheetName).Name Then
    SundarSheetExists = "Sheet already exists"
    Else
    SundarSheetExists = "Sheet is not available"
    End If
    Next
    End Function

  2. #2
    Registered User
    Join Date
    03-19-2019
    Location
    London
    MS-Off Ver
    2003-2016/2016 Mac
    Posts
    57

    Re: User Defined Function to check whether sheet name exists is not working

    Since input Parameter is a string, you need to wrap the sheet name in " " like "sheet1" while using the function in excel.

  3. #3
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: User Defined Function to check whether sheet name exists is not working

    Try this:

    Please Login or Register  to view this content.
    or this:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: User Defined Function to check whether sheet name exists is not working

    The OP problem is that the line
    Sheets(SheetName) throws an error in the "no sheet" case

    Try

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: User Defined Function to check whether sheet name exists is not working

    Quote Originally Posted by sundardrona View Post
    Please highlight what is the mistake in the code. It is able to return the value "Sheet already exists" when the sheet name exists. In else condition it throws #VALUE! error.

    Function SundarSheetExists(SheetName As String) As String
    For Each Sheet In Worksheets
    If SheetName = Worksheets(SheetName).Name Then
    SundarSheetExists = "Sheet already exists"
    Else
    SundarSheetExists = "Sheet is not available"
    End If
    Next
    End Function
    Mike is correct about where the error is coming from; however, the problem in your code's logic is what I highlighted in red... you are testing the SheetName argument against the wrong thing... your loop variable is Sheet, you should be testing againsts its name since you want to see if the SheetName argument equals the loop's currently iterated sheet's name. Try changing the If..Then statement it is in to this...
    Please Login or Register  to view this content.
    I would note that this test is case sensitive, so you might want to handle that by using this instead...
    Please Login or Register  to view this content.
    And one final point... once you find the match, you should stop the loop by using
    Please Login or Register  to view this content.
    otherwise if any sheets are iterated in the loop after the match is found, its name won't match and the function will be reset to "Sheet is not available".
    Last edited by Rick Rothstein; 09-24-2019 at 03:03 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. user-defined function to check spelling in a range
    By NickyC in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2018, 03:12 AM
  2. User defined function not working properly
    By xibo8748 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2017, 10:38 PM
  3. Replies: 2
    Last Post: 12-03-2015, 04:12 AM
  4. [SOLVED] If sheet exists then function, if not check next...Help
    By cheeze83 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-04-2013, 05:24 AM
  5. User Defined Function Not Working Without Quotations
    By PrizeGotti in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-03-2013, 11:38 AM
  6. [SOLVED] Check if sheet exists and delete it using IF ERROR function in VBA
    By Jardim in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-25-2012, 12:06 PM
  7. vlookup path not working in UDF (User Defined Function)
    By miagovino in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-15-2007, 01:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1