Help to code copy to 02 sheets extend copy to 100 sheets

sbv1986

Board Regular
Joined
Nov 2, 2017
Messages
75
Hi all
I have code below
Code work well If I filter, copy data from 06 sheets to sheet("ky1") with condition in sheet(main).range(G1) and to sheet(ky2) with condition in sheet(main).range(G2).

Now I want extend code that:
1. Condition value to filter in range: sheets(main).range(G1:G& lastrow)
2. For each condition above adding new sheets with name like: sheets(kyi) ==> i = 1 to lastrow
3. Filter and copy data form 6 sheets to sheets(kyi) if meet with condition at sheet(main).range(Gi)

With i from 50 to 150 (this mean form 50 to 150 sheet, condition must be copied) so I can't copy below code more time, code will too long.
Thanks and plesae see my code to clear idea
Code:
Sub Filter()Sheets("ky1").Cells.Clear
Sheets("ky2").Cells.Clear
Sheets("ky3").Cells.Clear
Dim iRow1 As Long, iRow2 As Long, iRow3 As Long, iRow4 As Long, iRow5 As Long, iRow6 As Long
Dim iRowb As Long, iRowc As Long, iRowd As Long
Dim ans1 As Long, ans2 As Long
Application.ScreenUpdating = False
ans1 = Sheets("MAIN").Range("G1").Value
ans2 = Sheets("MAIN").Range("G2").Value
    On Error Resume Next
iRow1 = Sheets("NB").Cells(Rows.Count, "A").End(xlUp).Row
iRow2 = Sheets("NGB").Cells(Rows.Count, "A").End(xlUp).Row
iRow3 = Sheets("G00854").Cells(Rows.Count, "A").End(xlUp).Row
iRow4 = Sheets("A00024").Cells(Rows.Count, "A").End(xlUp).Row
iRow5 = Sheets("G03654").Cells(Rows.Count, "A").End(xlUp).Row
iRow6 = Sheets("C00204").Cells(Rows.Count, "A").End(xlUp).Row
    With Sheets("NB").Range("A1:A" & iRow1)
         iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
         iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
         iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    With Sheets("NGB").Range("A1:A" & iRow2)
        iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
        iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
        iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    With Sheets("G00854").Range("A1:A" & iRow3)
        iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
        iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
        iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    With Sheets("A00024").Range("A1:A" & iRow4)
        iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
        iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
        iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    With Sheets("G03654").Range("A1:A" & iRow5)
        iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
        iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
        iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    With Sheets("C00204").Range("A1:A" & iRow6)
        iRowb = Sheets("ky1").Cells(Rows.Count, "A").End(xlUp).Row
        iRowc = Sheets("ky2").Cells(Rows.Count, "A").End(xlUp).Row
        iRowd = Sheets("ky3").Cells(Rows.Count, "A").End(xlUp).Row
        .AutoFilter Field:=1, Criteria1:="*" & ans1 & "*", Operator:=xlOr, Criteria2:=ans1
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky1").Range("A" & iRowb + 1)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="*" & ans2 & "*", Operator:=xlOr, Criteria2:=ans2
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("ky2").Range("A" & iRowc + 1)
        .AutoFilter
    End With
    On Error GoTo 0
Application.ScreenUpdating = True
End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Watch MrExcel Video

Forum statistics

Threads
1,118,887
Messages
5,574,839
Members
412,620
Latest member
sharma7s
Top