+ Reply to Thread
Results 1 to 6 of 6

Thread: New Sheets

  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    kampala, uganda
    MS-Off Ver
    Excel 2007
    Posts
    15

    New Sheets

    Hi friends,
    This problem might be simple, but it's been tedious to figure. Say there are five different values in a range of 10 cells .e.g. in range("A1:A10") there are values valueA, valueB, valueC, valueD, and valueE. this means these values get to be repeated randomly in all 10cells.
    I would like to identify, with a macro, the five different values in the range, and use these values to create new 5 sheets whose tab names are those 5 values discovered in the range. The idea then is to avoid the error that occurs when you rename one sheet with a sheet name already used by another sheet.
    I appreciate the help.
    Bryen Walt
    Last edited by bryenwalt; 03-30-2010 at 07:19 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: New Sheets

    Can the values in A1:A10 be sorted? If so you could then just loop through them checking if the cell you are on is the same as the one above and if so not create a sheet for it.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: New Sheets

    Try this.

    Sub Test()
    Dim SheetFound As Boolean
    Dim Cell As Range
    Dim Sheet As Worksheet
    
    For Each Cell In Range("A1:A10")
        SheetFound = False
        For Each Sheet In ActiveWorkbook.Sheets
            If Sheet.Name = Cell.Value Then
                SheetFound = True
                Exit For
            End If
        Next Sheet
        If SheetFound = False Then
            Sheets.Add after:=Sheets(Sheets.Count)
            ActiveSheet.Name = Cell.Value
        End If
    Next Cell
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: New Sheets

    Another way:
    Sub x()
        Dim cell As Range
    
        For Each cell In Range("A1:A10")
            If Len(cell.Text) Then
                If Not SheetExists(cell.Text) Then Worksheets.Add after:=Sheets(Sheets.Count)
                ActiveSheet.Name = cell.Text
            End If
        Next cell
    End Sub
    
    Function SheetExists(sWks As String, Optional wkb As Workbook) As Boolean
        On Error Resume Next
        SheetExists = Not IIf(wkb Is Nothing, ActiveWorkbook, wkb).Sheets(sWks) Is Nothing
    End Function
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    03-10-2010
    Location
    kampala, uganda
    MS-Off Ver
    Excel 2007
    Posts
    15

    Thumbs up Re: New Sheets

    MRICE, you code working brilliantly. SHG, you code does work, but produces the error "cannot rename a sheet to the same name as another sheet...". it's this error that was giving me alot of trouble. MRICE's code takes care of that error. Thanks guys, a burden has been lifted...

  6. #6
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: New Sheets

    Sorry for the bug. Corrected for anyone who might be interested.
    Sub x()
        Dim cell As Range
    
        For Each cell In Range("A1:A10")
            If Len(cell.Text) Then
                If Not SheetExists(cell.Text) Then
                    Worksheets.Add after:=Sheets(Sheets.Count)
                    ActiveSheet.Name = cell.Text
                End If
            End If
        Next cell
    End Sub
    
    Function SheetExists(sWks As String, Optional wkb As Workbook) As Boolean
        On Error Resume Next
        SheetExists = Not IIf(wkb Is Nothing, ActiveWorkbook, wkb).Sheets(sWks) Is Nothing
    End Function
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0