Loop in filter

luvbite38

Active Member
Joined
Jun 25, 2008
Messages
368
Hi guys,

is there a way in excel to open loop through a filtered column.

I mean a way to select the values in a filtered column A one by one.

example

Column A (header)

Dog
Cat
.
.
.
mouse

and macro to select the first value
and than second value
and loop until the last value

Kind Regards,

and thanks for all your help and assistance in advance:)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
Sub LoopRange()

For Each i In Range("A1:A10")
MsgBox (i)
Next i

End Sub
Thanks mate,

Thanks mate,

Could you help me slightly modify this code.

rather than showing the message box, can i some how do the following:

Apply Filter on range A1:C1

check the first value of the filtered list and copy the range A1:C1
than uncheck the first value and check the next value in the column A and copy the range A1:C1 and repeat until the last value in the cell.

Example

Animals Quality Quantity
Dog 2 5
Monkey 6 5
Cat 2 9
.
.
.
.
.
Rat 8 5

Desired

Filter to Dog >>>> Copy the visible range >>> paste in the another sheet (summary)>>>>go back to original sheet>>>> uncheck dog and filter to Monkey>>>> copy the visible range >>> paste in summary sheet>>>>> repeat the entire process until the last value in the column A of the original sheet.

Note: I will change the pasting part later on as it will conditionally paste the values to their relevant place. so for this purpose it can paste any where in summary sheet.

once again, I am really very thankful for your help

Kindest Regards,
 
Upvote 0
Hi this is the correct way to get the values of a Filtered range.
The first one will not work on a filtered range.
This collects the visible (filtered results) only.

Code:
Sub LoopRange()

Dim range1 As Range

With Sheet1
 Set range1 = .AutoFilter.Range.Offset(1, 0).Resize(.AutoFilter.Range.Rows.Count - 1) _
        .SpecialCells(xlCellTypeVisible)
        
         For Each i In range1
          MsgBox range1
         Next i

End With
End Sub

Also I cannot take credit for that code, the best bit I got from another source.
It uses the xlCellTypeVisible property and resize to resize the range into visible cells only.

Cheers
 
Last edited:
Upvote 0
oh my mistake,

columns are always fixed from A to C but the row nos. are variable.

also i ran your provided code and got following error
run time error 99

thx for your support.
 
Upvote 0
as i am totally new with vba, could you pls help me replace the msgbox part with the copy
and paste as value in first blank row of the summary sheet pls....

thanks a million
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,565
Members
449,237
Latest member
Chase S

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