Looping through filtered range not working

rizzo93

Active Member
Joined
Jan 22, 2015
Messages
303
Office Version
  1. 365
As my users use a spin button to move up/down through a column of IDs, my code finds that ID which is used in many other calculations in fields and cells. BTW, this spin button is in a userform.

When that column is not filtered, everything works fine. But when the column is filtered, I want my spin button to look for the ID that's in the next visible row. I've read several threads on this subject, but I'm still not able to make it all come together.

I think at least one of my problems is not knowing how to define these visible rows as a range.

These things I know for certain:
  • The first row in the range will always be >= row 11
  • Even though the last row will change, it will always be <= 150.
  • The column containing the IDs I'm interested in is column P

For now, I just want to figure out how to loop through the visible cells within a dynamic filtered range. Once I do, I believe I can figure out how to set the first and last row constraints.

I'd appreciate any help!

Code:
Sub testingrangeloop()
    For Each Rng In Selection.Areas <<< Normally, this will NOT be a manually selected area.
        For Each cl In Rng
        MsgBox cl.Value
       Next cl
    Next Rng
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
Code:
Sub rizzo93()
   Dim Rng As Range, Cl As Range
   
   For Each Rng In Range("P11:P150").SpecialCells(xlVisible).Areas
      For Each Cl In Rng
         MsgBox Cl.Address
      Next Cl
   Next Rng
End Sub
 
Upvote 0
I don't know English, I wrote with google translate. I'm sorry if I got it wrong.

Code:
Sub testingrangeloop()    
    lr = Cells(Rows.Count, "P").End(3).Row
    If lr > 150 Then lr = 150
    For Each Rng In Range("P11:P" & lr).SpecialCells(xlCellTypeVisible)
        MsgBox Rng.Value
    Next Rng
End Sub
 
Last edited:
Upvote 0
Hey Fluff, thanks for the assist.


Well, the code doesn't recognize which rows are visible; it goes through every single one of them.


For example, I've filtered some data to test it out. Under this circumstance, the range it should be looking through is $P$92:$P$101.
 
Upvote 0
What does the message box say with this?
Code:
Sub rizzo93()
   Dim Rng As Range, Cl As Range
   
   For Each Rng In Range("P11:P150").SpecialCells(xlVisible).Areas
         MsgBox Rng.Address & vbLf & Rng.Parent.Name
         Exit For
   Next Rng
End Sub
 
Upvote 0
I should clarify.

While the new range is $P$92:$P$101, it's still going through the entire range: $P$11:$P$150
 
Upvote 0
It begins with showing the address of the first cell: $P$11

I click Ok, then it shows the address for the next one: $P$12
And so on.
 
Upvote 0
I don't know English, I wrote with google translate. I'm sorry if I got it wrong.

Code:
Sub testingrangeloop()    
    lr = Cells(Rows.Count, "P").End(3).Row
    If lr > 150 Then lr = 150
    For Each Rng In Range("P11:P" & lr).SpecialCells(xlCellTypeVisible)
        MsgBox Rng.Value
    Next Rng
End Sub

Thanks, veyselemre.

This code still goes through the hidden cells. :(
 
Upvote 0
What does the message box say with this?
Code:
Sub rizzo93()
   Dim Rng As Range, Cl As Range
   
   For Each Rng In Range("P11:P150").SpecialCells(xlVisible).Areas
         MsgBox Rng.Address & vbLf & Rng.Parent.Name
         Exit For
   Next Rng
End Sub
Fluff,
Maybe this code below looks familiar to you because it's something you helped me with earlier. I added to it to achieve what I was trying to do, which was to create a string consisting of email addresses based on what cells were selected.
Code:
    For Each Rng In Selection.Areas
        For Each Cl In Rng
        projName = Cl
        distro = Application.WorksheetFunction.Index(Sheets("GhostData").Range("AV:AV"), Application.WorksheetFunction.Match(projName, Sheets("ghostdata").Range("C:C"), 0)) + "; " + distro
        projList = Cl + Chr(13) + projList
       Next Cl
    Next Rng

Instead of ctrl-clicking cells to define my range in that scenario, I now want to define it by what's left over (visible) from the filter that a user selects.

I'm not creating a string, but it's similar enough that I thought it may help.
 
Upvote 0
Care to answer the question in the post you just quoted? ;)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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