+ Reply to Thread
Results 1 to 5 of 5

Thread: Creat worksheets and name them from a range of cells

  1. #1
    Registered User
    Join Date
    08-08-2008
    Location
    Johannesburg
    MS-Off Ver
    2007
    Posts
    54

    Creat worksheets and name them from a range of cells

    Hi All
    I have a range cells (A1:A20) each have a word inside.

    How can I create a macro to generate/populate 20 worksheets that the names taken from the cells?

    Thanks in advance for your help

    Amit

    P.S
    This one doesn't help http://www.excelforum.com/excel-work...heet-name.html
    Last edited by furiousfox; 07-09-2009 at 01:27 PM. Reason: Update
    Using Excel 2007.
    Amit Cohen

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Creat worksheets and name them from a range of cells

    If we assume you're saying Sheet1!A1:A20 contains 20 words, you want to create a sheet for each word naming the sheet per the word ...

    Public Sub CreateSheets()
    Dim bSheet As Byte, strSName As String
    For bSheet = 1 To 20 Step 1
        strSName = Sheets("Sheet1").Cells(bSheet, "A")
        If LenB(strSName) Then
            Sheets.Add After:=Sheets(Sheets.Count)
            On Error GoTo SheetFail
            ActiveSheet.Name = strSName
            On Error GoTo 0
        End If
    ResumeHere:
    Next bSheet
    Exit Sub
    
    SheetFail:
    'either invalid naming convention or sheet exists already - delete newly added sheet
    Application.DisplayAlerts = False
    ActiveSheet.Delete
    Application.DisplayAlerts = True
    MsgBox "Failed to Create Sheet : " & strSName, vbCritical, "Error"
    Resume ResumeHere
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-03-2007
    Location
    Auckland, New Zealand
    MS-Off Ver
    2007
    Posts
    137

    Re: Creat worksheets and name them from a range of cells

    Give this a go..

    Sub Add_ManySheets()
    '
    '
    '
    
    ''' !!!! MAKE SURE YOU HAVE AN EMPTY ROW AT THE END OF YOUR LIST OF SHEET NAMES !!!!
    
    'Sheets("Your Sheet with Names you want for new Sheets").activate
    Sheets("ListOfSheetNames").Activate
    
    'First cell in your list of names
    
    Range("A3").Select 'CHANGE THIS TO "A1" IF THATS WHERE YOUR LIST OF SHEET NAMES STARTS
    
    'count the number of rows used in your list
    NumberOfNames = ActiveCell.CurrentRegion.Rows.Count
    
    'Create a For..Next Loop using the NumberOfNames
    For SheetAddLoop = 1 To NumberOfNames
    'Get cell contents to use for new sheetname, using the count of names to offest from the first name in the list
    MyNewSheetName = CStr(Sheets("ListofSheetNames").Range("A3").Offset(SheetAddLoop - 1, 0).Value)
        'add a new sheet after the last one
        Sheets.Add After:=Sheets(Sheets.Count)
        'select the new sheet
        ActiveSheet.Select
        'rename the new sheet as the
        ActiveSheet.Name = MyNewSheetName
        ActiveSheet.Range("A1").Select
        
    Next
    End Sub
    Should work fine - I added six new sheets using this.

    Beware !!! It's likely to crash if one of the sheet names is already used..

  4. #4
    Registered User
    Join Date
    08-08-2008
    Location
    Johannesburg
    MS-Off Ver
    2007
    Posts
    54

    Thumbs up Re: Creat worksheets and name them from a range of cells

    Hi DonkeyOte & Jbentley

    Thanks so much for the fast replay!!

    I have tested both scripts, and they works great


    Thanks for your help

    FuriousFox
    Using Excel 2007.
    Amit Cohen

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,208

    Re: Creat worksheets and name them from a range of cells

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ 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