+ Reply to Thread
Results 1 to 8 of 8

Function to check the sheet name exist or not based on sheet values

Hybrid View

narendrabr Function to check the sheet... 02-11-2014, 12:20 AM
patel45 Re: Function to check the... 02-11-2014, 02:39 AM
narendrabr Re: Function to check the... 02-11-2014, 03:02 AM
narendrabr Re: Function to check the... 02-12-2014, 03:00 AM
patel45 Re: Function to check the... 02-12-2014, 08:06 AM
narendrabr Re: Function to check the... 02-12-2014, 08:30 AM
patel45 Re: Function to check the... 02-12-2014, 09:34 AM
narendrabr Re: Function to check the... 02-12-2014, 09:37 AM
  1. #1
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Function to check the sheet name exist or not based on sheet values

    Hi,

    I need to check when one of the values in the column is equal to a worksheet that is currently in the excel file. Just a message box that says something like “A worksheet currently exists with the name of a value in this list. Please rename the current worksheet”. When the user gets the message box, they should be able to click “OK” and the function ends.
    Is it possible to modify this code?
    Option Explicit
    Option Compare Text
    Sub sheets_from_colvalues(control As IRibbonControl)
     
    'Const sh1 As String = "Sheet1"
    Dim sh1 As String
    sh1 = ActiveSheet.Name
    
    Dim a As Variant, x As Worksheet, sh As Worksheet
    Dim rws&, cls&, p&, i&, rr&, b As Boolean
    Dim cl&
    
    cl = Selection.Column
    Application.ScreenUpdating = False
    Sheets(sh1).Activate
    
    rws = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    cls = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    
    Set x = Sheets.Add(After:=Sheets(sh1))
    Sheets(sh1).Cells(1).Resize(rws, cls).Copy x.Cells(1)
    Set a = x.Cells(1).Resize(rws, cls)
    a.Sort a(1, cl), 2, Header:=xlYes
    a = a.Resize(rws + 1)
        p = 2
        
        For i = p To rws + 1
            If a(i, cl) <> a(p, cl) Then
                b = False
                
                For Each sh In Worksheets
                    If sh.Name = a(p, cl) Then b = True: Exit For
                Next
                If Not b Then
                   
                    With Sheets.Add
                      
                       .Name = a(p, cl)
                        x.Cells(1).Resize(, cls).Copy .Cells(1)
                        rr = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                        x.Cells(p, 1).Resize(i - p, cls).Cut .Cells(rr, 1)
                        .Columns.AutoFit
                    End With
                End If
                p = i
            End If
        Next i
    Application.DisplayAlerts = False
        x.Delete
    Application.DisplayAlerts = True
    Sheets(sh1).Activate
    Application.ScreenUpdating = True
    End Sub
    Thanks in advance!
    Last edited by narendrabr; 02-11-2014 at 12:39 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Function to check the sheet name exist or not based on sheet values

    can you attach a sample file ?
    you can use this code

    Sub M_snb()
        MsgBox shexist("sheet1b")
    End Sub
     
    Function shexist(c00)
        shexist = Evaluate("isref(" & c00 & "!A1)")
    End Function
    Last edited by patel45; 02-11-2014 at 02:43 AM.
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Function to check the sheet name exist or not based on sheet values

    Hi

    I have attached the macro enabled sheet.
    can you please modify the code to solve the above stated issue?

    Thanks!
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Function to check the sheet name exist or not based on sheet values

    Basically the execution should stop when the sheet name already exist and ask a message to rename the sheet with different name.
    Thanks

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Function to check the sheet name exist or not based on sheet values

    try this code
    Option Compare Text
    Sub sheets_from_colvalues()
     
    Lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = 2 To Lr
      shname = Cells(r, 1)
      If shexist(shname) Then
        Sheets(shname).Select
        MsgBox "A worksheet currently exists with the name of a value in this list. Please rename the current worksheet"
        Exit For
      End If
    Next
    End Sub
     
    Function shexist(c00)
        shexist = Evaluate("isref(" & c00 & "!A1)")
    End Function

  6. #6
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Function to check the sheet name exist or not based on sheet values

    Can you please modify my original code in the attached spreadsheet.
    Thanks!

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Function to check the sheet name exist or not based on sheet values

    I can not understand your code

  8. #8
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Function to check the sheet name exist or not based on sheet values

    Basically the code is splitting sheets based on column values.

+ 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. Replies: 4
    Last Post: 03-04-2012, 08:34 AM
  2. 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
  3. Check if Column Exist
    By izet99 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-21-2009, 02:43 PM
  4. VBA - Check if selected worksheets exist
    By Lucifix in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2009, 05:04 AM
  5. [SOLVED] Check if a value exist in a column
    By DJ Steffo in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-30-2006, 10:40 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