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

sbv1986

Board Regular
Joined
Nov 2, 2017
Messages
87
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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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