+ Reply to Thread
Results 1 to 3 of 3

Create New Sheet and copy data

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2018
    Location
    FL
    MS-Off Ver
    Office 365 Professional
    Posts
    28

    Create New Sheet and copy data

    Hello all,
    I have a spreadsheet (sample attached) and the code below that creates a new sheet named for each member and copies that data to the new sheet. This works as it should. However, I'd like for the child sheets to remain linked to the parent sheet so that updates can be entered in the parent ("loads") sheet and automatically update on the child sheets.

     Set My_Range = Range("A1:S" & LastRow(ActiveSheet))
        My_Range.Parent.Select
    
        If ActiveWorkbook.ProtectStructure = True Or _
           My_Range.Parent.ProtectContents = True Then
            MsgBox "Sorry, not working when the workbook or worksheet is protected", _
                   vbOKOnly, "Copy to new worksheet"
            Exit Sub
        End If
    
        'This example filters on the first column in the range(change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        FieldNum = 6
    
        'Turn off AutoFilter
        My_Range.Parent.AutoFilterMode = False
    
        'Change ScreenUpdating, Calculation, EnableEvents, ....
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        ActiveSheet.DisplayPageBreaks = False
    
        'Add a worksheet to copy the a unique list and add the CriteriaRange
        Set ws2 = Worksheets.Add
    
        With ws2
            'first we copy the Unique data from the filter field to ws2
            My_Range.Columns(FieldNum).AdvancedFilter _
                    Action:=xlFilterCopy, _
                    CopyToRange:=.Range("A1"), Unique:=True
    
            'loop through the unique list in ws2 and filter/copy to a new sheet
            lrow = .Cells(Rows.Count, "A").End(xlUp).Row
            For Each cell In .Range("A2:A" & lrow)
    
                'Filter the range
                My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
                 Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
    
                'Check if there are no more then 8192 areas(limit of areas)
                CCount = 0
                On Error Resume Next
                CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
                         .Areas(1).Cells.Count
                On Error GoTo 0
                If CCount = 0 Then
                    MsgBox "There are more than 8192 areas for the value : " & cell.Value _
                         & vbNewLine & "It is not possible to copy the visible data." _
                         & vbNewLine & "Tip: Sort your data before you use this macro.", _
                           vbOKOnly, "Split in worksheets"
                Else
                    'Add a new worksheet
                    Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
                    On Error Resume Next
                    WSNew.Name = cell.Value
                    If Err.Number > 0 Then
                        ErrNum = ErrNum + 1
                        WSNew.Name = "Error_" & Format(ErrNum, "0000")
                        Err.Clear
                    End If
                    On Error GoTo 0
    
                    'Copy the visible data to the new worksheet
                    My_Range.SpecialCells(xlCellTypeVisible).Copy
                    With WSNew.Range("A1")
                        ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                        ' Remove this line if you use Excel 97
                        .PasteSpecial Paste:=8
                        .PasteSpecial xlPasteValues
                        .PasteSpecial xlPasteFormats
                        Application.CutCopyMode = False
                        .Select
                    End With
                End If
    
                'Show all data in the range
                My_Range.AutoFilter Field:=FieldNum
    
            Next cell
    
            'Delete the ws2 sheet
            On Error Resume Next
            Application.DisplayAlerts = False
            .Delete
            Application.DisplayAlerts = True
            On Error GoTo 0
    
        End With
    
        'Turn off AutoFilter
        My_Range.Parent.AutoFilterMode = False
    
        If ErrNum > 0 Then
            MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
                 & vbNewLine & "There are characters in the name that are not allowed" _
                 & vbNewLine & "in a sheet name or the worksheet already exist."
        End If
    
        'Restore ScreenUpdating, Calculation, EnableEvents, ....
        My_Range.Parent.Select
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
        Dim xSh As Worksheet
        Application.ScreenUpdating = False
    Attached Files Attached Files
    Last edited by jmack46; 02-07-2023 at 06:25 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,975

    Re: Create New Sheet and copy data

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code] [/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    07-23-2018
    Location
    FL
    MS-Off Ver
    Office 365 Professional
    Posts
    28

    Re: Create New Sheet and copy data

    Thank you for that. I wondered how that worked. And, you're right, it is much easier to look at this way.

+ 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. [SOLVED] Create New Excel Sheet plus copy data from different sheet
    By czerwonywilk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2023, 02:33 PM
  2. Create a macro to to copy data from one sheet to another
    By nkap91 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2020, 09:24 AM
  3. create validation rule for unique data and copy data from other sheet
    By sarat47 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-02-2018, 07:34 PM
  4. Move data to new sheet and create copy of template sheet
    By jdm13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2014, 01:53 PM
  5. Macro: Create an input sheet an copy the data onto a new row in another sheet
    By bernardnoel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2013, 09:23 AM
  6. Copy Sheet / Create & Name New Sheet / Insert Before a Sheet / Paste Data
    By thinkspac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2012, 02:27 PM
  7. Filter, copy filtered data into another sheet, create a file of that sheet..
    By titushanke in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-17-2012, 02:26 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