Loop on Filter

aayaanmayank

Board Regular
Joined
Jul 20, 2018
Messages
157
Hi i have to apply filter then select one ID at one time then copy dates for the particular ID . then again select next id and repeat the same task till the last ID.

[TABLE="width: 191"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]Early Start[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]KOP[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABH[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]IKLPOYL[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]RAHUOP[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]WASPOCD[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]ABCXXXXZZZZ[/TD]
[TD]1-Aug-17[/TD]
[/TR]
[TR]
[TD]98KOP05[/TD]
[TD]1-Aug-17[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
where are the outputs going to? different sheets?

Need more information for this, expected result sample would be nice.
 
Upvote 0
so why not copy everything in (no loop) and sort by ID/date?
 
Upvote 0
Hi I have just given the sample data. Rest there are are multiple things which i have to do. Hence i only need support to filter one by one each id and copy the date and paste data.
 
Upvote 0
you didnt give expected results. try the below. it will copy the data over. it pastes in sheet1 X1. if you have data there change those references.

Code:
Sub test()
Dim sh As Worksheet, ws As Worksheet
    Set sh = Sheets("Sheet1")
    Set ws = Sheets("Sheet2")
        sh.Range("A1:A" & sh.Range("A" & Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sh.Range("X1"), Unique:=True
        
Dim rng As Range
    If sh.AutoFilterMode = True Then sh.AutoFilterMode = False
    For Each rng In sh.Range("X2:X" & sh.Range("X" & Rows.Count).End(xlUp).Row)
        sh.Range("A1:B" & sh.Range("A" & Rows.Count).End(xlUp).Row).AutoFilter 1, rng.Value
        sh.Range("A2:B" & sh.Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy ws.Range("A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row + 1)
        sh.AutoFilterMode = False
Next
End Sub
 
Upvote 0
needed to change one thing on the above

Code:
Sub test()
Dim sh As Worksheet, ws As Worksheet
    Set sh = Sheets("Sheet1")
    Set ws = Sheets("Sheet2")
        sh.Range("A1:A" & sh.Range("A" & Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sh.Range("X1"), Unique:=True
        
Dim rng As Range
    If sh.AutoFilterMode = True Then sh.AutoFilterMode = False
    For Each rng In sh.Range("X2:X" & sh.Range("X" & Rows.Count).End(xlUp).Row)
        sh.Range("A1:B" & sh.Range("A" & Rows.Count).End(xlUp).Row).AutoFilter 1, rng.Value
        sh.Range("A2:B" & sh.Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy ws.Range("A" & ws.Range("A" & Rows.Count).End(xlUp).Row + 1)
        sh.AutoFilterMode = False
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,832
Messages
6,181,235
Members
453,026
Latest member
cknader

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