Find matching data in column, paste to new created worksheet/tab

marcosis123

New Member
Joined
Dec 10, 2019
Messages
12
Office Version
  1. 365
Platform
  1. MacOS
Hi,

I have a table of data and in column C there are multiple entries of the same data.

I would like a macro to copy the rows containing matching values in C to a new sheet named the value of the data.

So if i had 6 rows that said aa,aa,bb,bb,cc,cc in column C id now have three tabs called aa, bb and cc with the 2 matching rows from the main table in each one.

Cheers
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
VBA Code:
Sub Copy2NewSheets()
    Dim wsAll As Worksheet
    Dim wsCrit As Worksheet
    Dim wsNew As Worksheet
    Dim LastRow As Long
    Dim LastRowCrit As Long
    Dim k As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    ' Change to main sheet name
    Set wsAll = Worksheets("Sheet1")
    ' Last row of column C
    LastRow = wsAll.Range("C" & Rows.Count).End(xlUp).Row
        
    ' Create a Temp sheet
    Set wsCrit = Worksheets.Add
        
    ' Column C has the criteria
    wsAll.Range("C1:C" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
    ' Last row of column A on temp sheet
    LastRowCrit = wsCrit.Range("A" & Rows.Count).End(xlUp).Row
    
    For k = 2 To LastRowCrit
        Set wsNew = Worksheets.Add
        wsNew.Name = wsCrit.Range("A2")
        wsAll.Rows("1:" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wsCrit.Range("A1:A2"), _
            CopyToRange:=wsNew.Range("A1"), Unique:=False
        wsCrit.Rows(2).Delete
    Next
        
    ' Delete Temp sheet
    wsCrit.Delete
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Another alternative:
VBA Code:
Sub CreateSheets()
    Application.ScreenUpdating = False
    Dim Rng As Range, WS As Worksheet, key As Variant
    Set WS = ThisWorkbook.Sheets("Sheet1")
    If WS.AutoFilterMode Then WS.AutoFilterMode = False
    With CreateObject("Scripting.Dictionary")
        For Each Rng In WS.Range("C2", WS.Range("C" & WS.Rows.Count).End(xlUp))
            .Item(Rng.Value) = Empty
        Next
        For Each key In .Keys
            With WS
                .Cells(1, 1).CurrentRegion.AutoFilter 3, key
                WS.AutoFilter.Range.Copy
                Worksheets.Add(After:=Sheets(Sheets.Count)).Name = key
                ActiveSheet.Cells(1, 1).PasteSpecial
            End With
        Next key
    End With
    WS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top