+ Reply to Thread
Results 1 to 5 of 5

Generate worksheets from list of values and have each worksheet contain the same formula

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2013, 2010, 2007, 2003
    Posts
    99

    Generate worksheets from list of values and have each worksheet contain the same formula

    Greetings,

    I have a macro that creates worksheets based on the values in column A; 1 tab for each value with that value as the tabs name.

    The next step, where I'm completely lost, is to have each of these new tabs be a copy of the "tab setup" worksheet with the value in cell B1 matching the tab name.

    I've attached a workbook to help explain my desired outcome, any and all assistance is greatly appreciated.

    tab setup for excel forum.xlsm


    Current code is shown below:
    Sub Generate_Tabs()
    
    Dim strCol As String
    Dim strRow As String
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    Dim strWsName As String
    Dim strSrcName As String
    
    On Error GoTo ErrHnd
    
    
    strCol = "A"
    strRow = "2"
    
    Application.ScreenUpdating = False
                
    strSrcName = ActiveSheet.Name
    
    Set rngStart = ActiveSheet.Range(strCol & strRow)
    Set rngEnd = ActiveSheet.Range(strCol & CStr(Application.Rows.Count)).End(xlUp)
                
    For Each rngCell In ActiveSheet.Range(rngStart, rngEnd)
        If rngCell.Text <> "" Then
            strWsName = rngCell.Text
            On Error Resume Next
            If Worksheets(strWsName) Is Nothing Then
                On Error GoTo ErrHnd
                Worksheets.Add After:=Worksheets(Worksheets.Count)
                Worksheets(Worksheets.Count).Name = strWsName
                Else
                On Error GoTo ErrHnd
            End If
        End If
    Next rngCell
    
    Worksheets(strSrcName).Activate
    
    Application.ScreenUpdating = True
    Exit Sub
    
    ErrHnd:
    Err.Clear
    Worksheets(strSrcName).Activate
    Application.ScreenUpdating = True
    
    End Sub
    SPARTAN
    Please click the * if my solution helped

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Generate worksheets from list of values and have each worksheet contain the same formu

    Why not copy the 'tab setup' sheet?
    Sub Generate_Tabs()
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    
        Application.ScreenUpdating = False
    
        strSrcName = ActiveSheet.Name
    
        Set rngStart = Worksheets("input").Range("A2")
        Set rngEnd = Worksheets("input").Range("A" & Application.Rows.Count).End(xlUp)
    
        For Each rngCell In Worksheets("input").Range(rngStart, rngEnd)
            If rngCell.Text <> "" Then
                strWsName = rngCell.Text
    
                Worksheets("tab setup").Copy after:=Worksheets(Worksheets.Count)
    
                With Worksheets(Worksheets.Count)
                    .Name = strWsName
                    .Range("B1").Value = .Name
                End With
            End If
        Next rngCell
        
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Generate worksheets from list of values and have each worksheet contain the same formu

    Or this one
    Attached Files Attached Files

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Generate worksheets from list of values and have each worksheet contain the same formu

        Dim Rws As Long, Rng As Range, c As Range
        Dim sht As Worksheet
        Set sht = Worksheets("input")
    
        With sht
            Rws = .Cells(Rows.Count, "A").End(xlUp).Row
            Set Rng = Range(.Cells(2, 1), .Cells(Rws, 1))
        End With
        For Each c In Rng.Cells
            Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = c
            Range("A1") = "ID": Range("A3") = "Name": Range("A4") = "Age": Range("B1") = c
            Range("B3") = c.Offset(0, 1): Range("B4") = c.Offset(0, 2)
        Next c

  5. #5
    Registered User
    Join Date
    04-20-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2013, 2010, 2007, 2003
    Posts
    99

    Re: Generate worksheets from list of values and have each worksheet contain the same formu

    Thank you all! I'm running through each but they all appear to do exactly what I want.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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