+ Reply to Thread
Results 1 to 6 of 6

Check if sheet name already exist

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-20-2017
    Location
    Portugal
    MS-Off Ver
    2016
    Posts
    118

    Check if sheet name already exist

    Hello, i have this segment of the code that creates a sheet named with the textbox1.value.

    I want it to check first if that sheet already exists, if so dont create the sheet and continue the code.

    Dim Sheetname As String
    Dim ws As Worksheet
    
    Sheetname = TextBox1.value
    If Sheetname <> "" Then
    Set ws = ThisWorkbook.Sheets.Add(after:=Sheets(Sheets.Count))
    ws.Name = Sheetname
    End If
    hope u can help me

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Check if sheet name already exist

    Try this code
    Sub Test()
        Dim ws          As Worksheet
        Dim SheetName   As String
    
        SheetName = TextBox1.Value
        
        If SheetName <> "" And Not Evaluate("ISREF('" & SheetName & "'!A1)") Then
            Set ws = ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count))
            ws.Name = SheetName
        Else
            MsgBox "Exists", vbExclamation
        End If
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Contributor
    Join Date
    04-20-2017
    Location
    Portugal
    MS-Off Ver
    2016
    Posts
    118

    Re: Check if sheet name already exist

    thank you, works like a charm, bu t i still dont get that part.

    can you explain that section?
     If SheetName <> "" And Not Evaluate("ISREF('" & SheetName & "'!A1)") Then
    i realy like to understand the code and what it does insted of just copyu paste :D

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Check if sheet name already exist

    You're welcome
    ISREF is a worksheet function which can be used in worksheet
    =ISREF(Sheet1!A1)
    It returns True if Sheet1 exists .. and if you change "Sheet1" to non-existing sheet, a window of "Update Values" will popup as an evidence that the sheet is not existing so this returns True or False
    And using Not reverse the boolean value ..
    so if the sheet doesn't exist so this part would return 'False' and Not False =True then the lines after the condition line will be executed
    Regards

  5. #5
    Forum Contributor
    Join Date
    04-20-2017
    Location
    Portugal
    MS-Off Ver
    2016
    Posts
    118

    Re: Check if sheet name already exist

    got it :D

    thanks for your help

    Best Regards

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Check if sheet name already exist

    You're welcome. Glad I can offer some help
    please mark the thread as solved

+ 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. Before run code Check first if sheet name exist or not
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2017, 06:19 AM
  2. Check if sheet exist based on drop down list
    By oerberg in forum Excel General
    Replies: 3
    Last Post: 03-23-2016, 11:55 AM
  3. [SOLVED] Check First IF Sheet Is Exist With The Name Of Cell K7 If exist Then run the code
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-18-2015, 10:08 AM
  4. [SOLVED] check column headers against sheet names if sheet not exist create it
    By AkaTrouble in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2015, 05:59 PM
  5. Function to check the sheet name exist or not based on sheet values
    By narendrabr in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-12-2014, 09:37 AM
  6. macro to check if a value exist in sheet and add it if it doesnt
    By RetroGun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2011, 02:35 AM
  7. macro to check if a value exist in sheet and add it +other parts of row if it doesnt
    By RetroGun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2011, 07:14 AM

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