VBA to Filter Data and copy to existing Tab

Trebor200

Board Regular
Joined
Apr 21, 2015
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Looking for a better way to filter data and copy to an existing tab and clear existing data beforehand.

I am currently doing this by recording VBA , filtering, selecting tab, pasting etc feel there may be a better / quicker way of running this.

Below is the code i have repeating each time for each known product, all the tabs are created and wish to cycle through the All Data Sheet and copy data to tabs.


Data Example below. number of parts and tabs will grow over time and not limited to what is currently in the list.

VBA Code:
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
        "ABC123"

    Sheets("ABC123").Select
    Rows("3:3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp

    Sheets("All Data").Select
    Range("A9:I10").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveWindow.SmallScroll Down:=-10
    Sheets("ABC123").Select
    Range("A2").Select
    ActiveSheet.Paste
    Sheets("All Data").Select
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1

MrExcel Help.xlsx
ABCDEFGHI
1ProductAccountProduct TraceNumberStatusRequired# DaysEscaDelivery
2DEF456166672549002875429364022/06/202146119/02/2021
3DEF456166677773002876568564026/07/202143723/02/2021
4GHI7891671539410029834077115017/10/202212818/06/2021
5ABC1231675726210030747305198011/11/202210908/11/2021
6ABC1231675726220030747316164026/01/20236408/11/2021
7ABC1231675726230030747327161021/02/20234608/11/2021
8ABC1231676014650030810040172013/01/20237316/11/2021
9ABC1231676014660030810048161021/02/20234616/11/2021
10ABC1231676556540030943727178005/01/20237902/12/2021
11ABC1231676556550030943728178011/04/20231302/12/2021
12ABC1231681359230032055746178009/01/20237702/05/2022
13ABC1231681359240032055753164502/03/20233902/05/2022
14ABC1231681747520032138080172006/02/20235726/05/2022
15ABC1231684357400032712322164026/01/20236402/08/2022
16ABC1231684357530032712443161021/02/20234608/08/2022
17ABC1231684357540032712448161021/02/20234605/08/2022
18ABC1231684357560032712455161021/02/20234608/08/2022
19ABC1231684357570032712463164011/04/20231309/08/2022
20ABC1231684357590032712481178011/04/20231308/08/2022
21ABC1231685503790032984078172027/04/2023112/09/2022
22ABC1231686521640033214774168028/04/202306/10/2022
23ABC1231686521660033214780164706/03/20233707/10/2022
24ABC1231686847370033288894161021/02/20234614/10/2022
25ABC123168743629003342143942031/05/202222602/11/2022
26ABC123168768577003348232942004/07/202220208/11/2022
27ABC123168790302003352861042012/07/202219615/11/2022
28ABC123168893646003376193042001/08/202218214/12/2022
29ABC123168960006003391261342028/07/202218413/01/2023
30GHI789168970867003393504418005/10/202213613/12/2022
31GHI789168970872003393509318005/10/202213609/12/2022
32DEF456169034645003409113814021/09/202214631/01/2023
33DEF456169103566003423526613029/09/202214022/02/2023
34123ABC169345236003476332031417/04/2023913/12/2022
35123ABC169345238003476333240027/02/20234213/12/2022
36123ABC169345239003476333739015/03/20233013/12/2022
37DEF456169402021003488762214024/02/20234312/04/2023
38ABC123169669822003550551342016/02/20234912/07/2023
39ABC123169669823003550551741013/02/20235213/07/2023
40ABC123169669824003550551942007/02/20235630/06/2023
41ABC123169669826003550552442014/02/20235112/07/2023
42ABC123169669827003550552842029/03/20232030/06/2023
43GHI789169751649003567217922003/04/20231704/03/2024
44GHI789169868564003593148713530/03/20231920/03/2024
All Data
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello.
Assuming it is NOT a coincidence that the data you filter on column A and the name of the sheet receiving the copy are the same, then you can use the following:

VBA Code:
Sub Macro_8()
  Application.ScreenUpdating = False
  copy_Routine Range("Table1").ListObject, "ABC123"
End Sub

Private Sub copy_Routine(tbl As ListObject, iName$)
Dim ws As Worksheet
If Evaluate("IsRef('" & iName & "'!A1)") Then Set ws = Worksheets(CStr(iName)) Else Set ws = Worksheets.Add(, ActiveSheet)
With ws
  .Name = iName: .Cells(1).CurrentRegion.Delete xlShiftUp
  .Range("A2") = "=" & tbl.Range(2, 1).Address(0, 0, , True) & " = """ & iName & """"
  tbl.Range.AdvancedFilter 2, .Range("A1:A2"), .Range("A3"), False
  .Rows("1:2").Delete: .Cells(1).CurrentRegion.Font.ColorIndex = xlAutomatic
  .Cells(1).CurrentRegion.EntireColumn.AutoFit
End With
End Sub
 
Upvote 0
Please try this

I created a table per below with a formula. This creates a new sheet if the sheet name for the product doesn't exist. The macro filters, then copies the data to each sheet

VBA Code:
Sub UpdateProductCodeSheets()
  Dim TWB As Workbook
  Dim Rng As Range
  Dim OutRng As Range
  Dim Cel As Range
  Dim CC As Range
  Dim ASht As Worksheet
  Dim Sht As Worksheet
  Dim LO As ListObject
  Dim UPL As Range
  Dim SetupSht As Worksheet
  Dim Prod As String
  Dim ProdSht As Worksheet
  Dim Hdrs As Range
  
  
  Set TWB = ThisWorkbook
  Set ASht = TWB.Worksheets("All Data")
  Set Hdrs = ASht.Range("Table1[#Headers]")
  Set LO = ASht.ListObjects("Table1")
  Set SetupSht = TWB.Sheets("Setup")
  Set UPL = SetupSht.Range("UniqueProducts_List")
  
  For Each Cel In UPL
    Prod = Cel.Value
    LO.Range.AutoFilter Field:=1, Criteria1:=Prod  'Filter
    Set Rng = ASht.Range("Table1").SpecialCells(xlCellTypeVisible)                 'Table range
    Set Sht = Nothing
    On Error Resume Next
    Set Sht = TWB.Sheets(Prod)
    On Error GoTo 0
    If Not Sht Is Nothing Then
      Set ProdSht = Sht
    Else
      Sheets.Add After:=ASht
      Set ProdSht = ActiveSheet
      ProdSht.Name = Prod
      Hdrs.Copy ProdSht.Range("A2")
    End If
    
    Set CC = ProdSht.Range("3:3")
    Set OutRng = ProdSht.Range(CC, CC.End(xlDown))
    OutRng.Value = ""
    Set CC = ProdSht.Range("A3")
    Rng.Copy CC
    'Set OutRng = ProdSht.Range(CC, CC.Offset(Rng.Rows.Count - 1, Rng.Columns.Count - 1))
    'OutRng.Value = Rng.Value
    
  Next Cel
  
  
  
End Sub

Book1 20230502.xlsm
C
1Unique Products
2DEF456
3GHI789
4ABC123
5123ABC
Setup
Cell Formulas
RangeFormula
C2:C5C2=UNIQUE(Table1[Product])
Dynamic array formulas.
 
Upvote 0
Another option. Try it on a copy of your data.

VBA Code:
Option Explicit
Sub Trebor200()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("All Data")
    Dim d As Object, arrIn, arrOut, i As Long, shtName As String
    Set d = CreateObject("scripting.dictionary")
    arrIn = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))
    
    For i = 1 To UBound(arrIn, 1)
        d(arrIn(i, 1)) = 1
    Next i
    ReDim arrOut(1 To d.Count, 1 To 1)
    arrOut = d.keys
        
    Dim rCrit As Range, rCopyTo As Range
    For i = LBound(arrOut) To UBound(arrOut)
        shtName = arrOut(i)
        If WorksheetExists(shtName) Then
            Set ws2 = Worksheets(shtName)
            ws2.UsedRange.ClearContents
        Else
            ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = shtName
            Set ws2 = Worksheets(shtName)
        End If
        
        Set rCopyTo = ws2.Range("A1").Resize(, 9)
        With ws1.Range("A1").CurrentRegion
            Set rCrit = .Offset(, .Columns.Count + 1).Resize(2, 1)
            rCrit.Cells(1, 1).Value2 = "Product"
            rCrit.Cells(2, 1).Value2 = shtName
            .AdvancedFilter xlFilterCopy, rCrit, rCopyTo
        End With
        rCrit.Cells(1).Resize(2, 1).ClearContents
    Next i
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
    Dim sht As Worksheet
    If wb Is Nothing Then Set wb = ThisWorkbook
    On Error Resume Next
    Set sht = wb.Sheets(shtName)
    On Error GoTo 0
    WorksheetExists = Not sht Is Nothing
End Function
 
Upvote 0
Solution
Thanks All, appreciate the feedback, tried a couple and Kevin9999 solution to work... Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

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