+ Reply to Thread
Results 1 to 5 of 5

VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2021
    Location
    US
    MS-Off Ver
    Excel 365
    Posts
    58

    VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

    I receive an excel file each week which I have to create a sheet for each Team along with the data, and name the tab according to the Team.

    So for instance, my steps each week are as follows:
    1) From the 'All Data' sheet, filter column A for 'Carter Team',
    2) Copy the visible rows including the header,
    3) Paste this data to a new sheet within the workbook (paste at top, starting in A1),
    4) Rename the sheet to 'Carter Team'.
    5) Go back to the 'All Data' sheet and repeat until all teams have an separate tab.

    I do these steps for each team in column A which varies from 7 to 11 teams per week. I would like code for doing these steps but unsure of the code since the number of teams vary and don't want a tab for a team if there is no data for that week.

    Information about the 'All Data' sheet:
    - Row 3 is the Header (column A thru column AP)
    - Data starts in row 4
    - Teams are in column A (number of teams vary)
    - Last row of data varies
    Note: There may be instances where a row does not have a team assigned in column A so team is blank (rare but happens). There is data in every row in column K.

    Your knowledge and help is appreciated!
    Last edited by ellenlewis; 02-05-2024 at 11:16 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,992

    Re: VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

    Here is code that I use to do that task - this is based on splitting by column A, with headers in the first row of the current region based on A3 (so, at least, row 2 should be fully blank or emptied before running the code)


    Sub SplitDataBase()
        Dim C As Range
        Dim DSh As Worksheet
        Dim ASh As Worksheet
        Dim strName As String
        Dim rngC As Range
        Dim lngKC As Long
        
        'Optional code to select key column
        'Set rngC = Application.InputBox("Select a cell in the key column", Type:=8)
        'lngKC = rngC.Column
        
        'Code to specify key column
        lngKC = 1 'Key Column 1 = A, 2 = B etc.
        
        Application.DisplayAlerts = False
        Application.EnableEvents = False
    
        Set ASh = ActiveSheet
        Set rngC = ASh.Range("A3").CurrentRegion
        With ASh
            rngC.Columns(lngKC).AdvancedFilter Action:=xlFilterCopy, _
                CopyToRange:=.Cells(.Rows.Count, lngKC).End(xlUp)(3), Unique:=True
            With .Cells(.Rows.Count, lngKC).End(xlUp).CurrentRegion
                For Each C In .Cells.Offset(1).Resize(.Cells.Count - 1, 1)
                    If C.Value <> "" Then
                        On Error Resume Next
                        Worksheets(C.Value).Delete
                        On Error GoTo 0
                        Set DSh = Worksheets.Add(after:=Worksheets(Worksheets.Count))
                        DSh.Name = C.Value
                        rngC.AutoFilter Field:=lngKC, Criteria1:=C.Value & "*"
                        rngC.SpecialCells(xlCellTypeVisible).Copy DSh.Range("A1")
                        rngC.AutoFilter
                        DSh.Cells.EntireColumn.AutoFit
                    End If
                Next C
                .Clear
            End With
        End With
        
        If MsgBox("Export the new sheets to files?", vbYesNo) = vbYes Then
            For Each DSh In ActiveWorkbook.Worksheets
                If DSh.Name <> ASh.Name Then
                    DSh.Move
                    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Workbook " & ActiveSheet.Name & ".xlsx"
                    ActiveWorkbook.Close
                End If
            Next DSh
        End If
    
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,338

    Re: VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

    No sample file so untested...
    Sub J3v16()
    Dim Data, Dict As Object, Rng As Range, i As Long
    Set Dict = CreateObject("Scripting.Dictionary")
    With Sheets("All Data")
        With .Range("A3:AP" & .Cells(.Rows.Count, 1).End(xlUp).Row)
            Data = .Value
            For i = 2 To UBound(Data)
                If Data(i, 1) <> "" Then
                    If Not Dict.exists(Data(i, 1)) Then
                        Dict.Add Data(i, 1), "'"
                        .AutoFilter 1, Data(i, 1)
                        Set Rng = .SpecialCells(12)
                        .AutoFilter
                        If Evaluate("ISREF('" & "" & Data(i, 1) & "" & "'!A1)") = True Then
                            Application.DisplayAlerts = False
                            Sheets("" & Data(i, 1) & "").Delete
                            Application.DisplayAlerts = True
                        End If
                        With Sheets.Add(, Sheets(Sheets.Count))
                            .Name = Data(i, 1)
                            Rng.Copy .Cells(1)
                        End With
                    End If
                End If
            Next i
        End With
    End With
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  4. #4
    Registered User
    Join Date
    05-22-2021
    Location
    US
    MS-Off Ver
    Excel 365
    Posts
    58

    Re: VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

    Awesome! Codes works perfectly!

  5. #5
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,338

    Re: VBA Needed to Create New Sheet With Data in Workbook for Each Team in Column A

    Without sample file...okie dokie...Glad to have contributed...Tx for rep +

+ 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: 1
    Last Post: 07-15-2022, 06:32 PM
  2. [SOLVED] Macro needed for AutoScroll Down - Block up Block (TV Display team by team) - Help Plz
    By NametobeRenamed in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2019, 12:03 AM
  3. [SOLVED] VBA Help needed to create sheet based on master sheet column cell values
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-12-2016, 04:58 PM
  4. [SOLVED] VBA Help needed to create separate sheets for each team by year
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-05-2014, 12:42 PM
  5. Replies: 1
    Last Post: 12-03-2013, 04:19 AM
  6. guys i am trying to create a team selection sheet that has to do 3 teams
    By Bren1987 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-29-2012, 12:34 PM

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