+ Reply to Thread
Results 1 to 12 of 12

Confirm Worksheet does not exist

Hybrid View

  1. #1
    Registered User
    Join Date
    08-03-2012
    Location
    Cork Ireland
    MS-Off Ver
    Excel 2007
    Posts
    66

    Confirm Worksheet does not exist

    Hi All (again) lol

    i have a quick query, looking to do the following,

    i have a piece of code that names a new work sheet after a cell value.

    if the user enters the same date that has a sheet that already exists in the workbook then it crashes out with the "same name" error.

    What i have been trying to do is implement a check to see if the cell value which i pass to a string already exists. but im not sure how to check all worksheets and see if the name is already there.

    if someone could give me a pointer it would be greatly appreciated.

    many thanks
    Dave

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Confirm Worksheet does not exist

    Dim cel As String
    cel = Range("A1") ' name of the cell
            If Not Evaluate("ISREF('" & cel & "'!A1)") Then
    
               Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = cel
            Else
            
              MsgBox "The sheet has alredy existed"
            End If
    Last edited by AB33; 07-10-2014 at 06:35 AM.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Confirm Worksheet does not exist

    Hi, Dave,

    If Not Evaluate("ISREF('" & Sheets("Sheet1").Range("A1").Value & "'!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Sheets("Sheet1").Range("A1").Value
    Else
      MsgBox "The sheet already exists"
    End If
    Ciao,
    olger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    08-03-2012
    Location
    Cork Ireland
    MS-Off Ver
    Excel 2007
    Posts
    66

    Re: Confirm Worksheet does not exist

    RIght im totally lost,

    my code below, as it has been given by someone working with me for review, i simply added a sheet name part as they recorded the whole thing and i did a quick tidy and added a quick enter date check at the start.




    If Range("B2:C2").Text = "" Then 'B2:C2 is passed to A50 and and this cell is used to name the sheet.
        MsgBox "Please enter Date in B2"
        Exit Sub
               
        Else
            
        ActiveSheet.Unprotect
        Sheets(1).Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = Format(Range("A50"), "dd-mm-yyyy")
        
        Sheets(Sheets.Count).Protect
        Sheets(1).Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.ClearContents
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:= _
        False
        End If
        
        ActiveWorkbook.Save

    I thought the check would have been something similar to

    Dim wkshtnme as string
    wkshtnme = Format(Range("A50"), "dd-mm-yyyy")
    
    if worksheets.name = wkshtnme then 'here is where i thought there might be a straight forward line to solve this.
    msgbox "this name exists, reenter name"
    exit sub
    else
    'perform the bulk of the code above...


    should i be using an error check or a do while loop?

    i relatively new to vba and spend a good bit of time with vb.net but im a total novice...

    so im happy for all advice as i like to try and get there myself in the end
    Last edited by unreal_event_horizon; 07-10-2014 at 06:50 AM.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,962

    Re: Confirm Worksheet does not exist

    In a new workbook, Sheet1 will exist and Sheet4 will not exist.

    Option Explicit
    
    Function fSheetExists(shName As String)
    Dim wsSheet As Worksheet
    On Error Resume Next
    Set wsSheet = Sheets(shName)
    On Error GoTo 0
    If Not wsSheet Is Nothing Then
        fSheetExists = True
    Else
        fSheetExists = False
    End If
    
    End Function
    
    Sub Test_fSheetExists()
    
    MsgBox "Sheet1 " & IIf(fSheetExists("Sheet1"), "exists", "does not exist")
    MsgBox "Sheet4 " & IIf(fSheetExists("Sheet4"), "exists", "does not exist")
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Confirm Worksheet does not exist

    Option Explicit
    Sub test1()
    
    Dim wkshtnme As String
    wkshtnme = Format(Range("A50"), "dd-mm-yyyy")
            If Not Evaluate("ISREF('" & wkshtnme & "'!A1)") Then
    
               Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = wkshtnme
                 MsgBox "this name exists, reenter name"
                Exit Sub
            Else
              MsgBox "The sheet has alredy existed"
           
            End If
    End Sub
    Last edited by AB33; 07-10-2014 at 06:59 AM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,962

    Re: Confirm Worksheet does not exist

    With the function that I provided, your code would look like:

    Sub Test()
    
    Dim wkshtnme As String
    wkshtnme = Format(Range("A50"), "dd-mm-yyyy")
    
    If fSheetExists(wkshtnme) Then 'here is where i thought there might be a straight forward line to solve this.
        MsgBox "this name exists, reenter name"
        Exit Sub
    Else
        'perform the bulk of the code above...
    End If
    End Sub

    Regards, TMS

  8. #8
    Registered User
    Join Date
    08-03-2012
    Location
    Cork Ireland
    MS-Off Ver
    Excel 2007
    Posts
    66

    Re: Confirm Worksheet does not exist

    have it working now, many thanks for all your input.

    I have a question if anyone wants to give me a quick explanation. the code in the tags is the working code.

    but i want to know in basic english what this is telling me.....

    If Not Evaluate("ISREF('" & wkshtnme & "'!A1)") Then
    am i right in thinking, if the sheet name does not exist then.......

    could i be so rude to ask for this line to be broken down better than that?



        If Range("B2:C2").Text = "" Then
        MsgBox "Please enter Date in B2"
        Exit Sub
               
        Else
               
        Dim wkshtnme As String
        
        wkshtnme = Format(Range("A50"), "dd-mm-yyyy")
        
        If Not Evaluate("ISREF('" & wkshtnme & "'!A1)") Then
    
        ActiveSheet.Unprotect
        Sheets(1).Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = wkshtnme
        Sheets(Sheets.Count).Protect
        Sheets(1).Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.ClearContents
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:= _
        False
        Else
        MsgBox "The sheet Name Already exists" & vbNewLine & "Please re-enter a newdate"
        Exit Sub
        
        
        End If
        End If
        ActiveWorkbook.Save
    Last edited by unreal_event_horizon; 07-10-2014 at 07:08 AM. Reason: i cant spell in plain english, lol

  9. #9
    Registered User
    Join Date
    08-03-2012
    Location
    Cork Ireland
    MS-Off Ver
    Excel 2007
    Posts
    66

    Re: Confirm Worksheet does not exist

    Quote Originally Posted by TMS View Post
    With the function that I provided, your code would look like:

    Sub Test()
    
    Dim wkshtnme As String
    wkshtnme = Format(Range("A50"), "dd-mm-yyyy")
    
    If fSheetExists(wkshtnme) Then 'here is where i thought there might be a straight forward line to solve this.
        MsgBox "this name exists, reenter name"
        Exit Sub
    Else
        'perform the bulk of the code above...
    End If
    End Sub

    Regards, TMS
    apologies for not testing, this TMS, when creating a function, do i create a new module, save the function in there and call it back like any other macro by name when i want to use it?

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Confirm Worksheet does not exist

    In plain English yes

  11. #11
    Registered User
    Join Date
    08-03-2012
    Location
    Cork Ireland
    MS-Off Ver
    Excel 2007
    Posts
    66

    Re: Confirm Worksheet does not exist

    Reputation added to all, many thanks for your input

    Dave

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,962

    Re: Confirm Worksheet does not exist

    You're welcome. Thanks for the rep.


    when creating a function, do i create a new module, save the function in there and call it back like any other macro by name when i want to use it?
    Essentially, yes. Doesn't have to be in its own module but I consider it best practice. If you rename the module, it's easier to find things.

    Regards, TMS

+ 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. [SOLVED] how to run macro or msgbox when Worksheet does not exist/exist
    By herukuncahyono in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-14-2013, 10:54 PM
  2. [SOLVED] using macro to check worksheet if data input in another worksheet exist.
    By Curtis goh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-30-2013, 01:02 AM
  3. i need a macro if a worksheet name exist rename to a new worksheet
    By daillest319 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2012, 04:55 PM
  4. Replies: 4
    Last Post: 06-18-2006, 01:10 PM
  5. [SOLVED] Confirm before deleting a worksheet?
    By edeil in forum Excel General
    Replies: 1
    Last Post: 01-27-2006, 10:50 PM

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