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
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