Select every nth row in filtered range

neqkeet

New Member
Joined
Nov 21, 2016
Messages
15
Hi everyone.

I need to select every 2nd row in filered table. For example, if visible rows are as in the table below, i need to select 5th, 10th and 19th. Thx for your help

[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]*Row number*[/TD]
[TD]*Column A Header[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]25[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]



Code:
Sub Eeeeeee()Dim rng As Range
Dim InputRng As Range
Dim InputRngArea As Range
Dim OutRng As Range
Dim xInterval As Integer
Set InputRng = Range("C1:C200")
xInterval = 2
For i = 2 To InputRng.Rows.Count Step xInterval
    Set rng = InputRng.Cells(i, 1)
    If OutRng Is Nothing Then
        Set OutRng = rng
    Else
        Set OutRng = Application.Union(OutRng, rng)
    End If
Next
OutRng.EntireRow.Select
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
What are you going to do with them after selecting?
I ask because you rarely need to actually select cells/ranges to work with them and selecting can slow your code considerably.
 
Upvote 0
What are you going to do with them after selecting?
I ask because you rarely need to actually select cells/ranges to work with them and selecting can slow your code considerably.

Going to delete them. But i prefer to select them first, to be sure that code worked correctly. However if selecting will slow the code, it is better to highlight them.
 
Last edited:
Upvote 0
How about
Code:
Sub Eeeeeee()

    Dim Chk As Boolean
    Dim OutRng As Range
    Dim Cnt As Long
    
    For Cnt = 2 To 2000
        If Not Rows(Cnt).Hidden Then
            If Not Chk Then
                If OutRng Is Nothing Then
                    Set OutRng = Range("A" & Cnt)
                Else
                    Set OutRng = Union(OutRng, Range("A" & Cnt))
                End If
                Chk = True
            Else
                Chk = False
            End If
        End If
    Next
    OutRng.EntireRow.Interior.Color = vbRed

End Sub
 
Upvote 0
Another option, just dealing with the visible rows:
Code:
Sub EveryNth()
  Dim InputRng As Range, OutRng As Range, VisRng As Range, c As Range
  Dim i As Long
  
  Const xInterval As Long = 2
  
  Set InputRng = Range("C1:C200")
  Set VisRng = InputRng.SpecialCells(xlVisible)
  If VisRng.Cells.Count > 1 Then
    Set OutRng = InputRng.Cells(1)
    i = -1
    For Each c In VisRng
      i = i + 1
      If i Mod xInterval = 0 Then Set OutRng = Union(OutRng, c)
    Next c
  End If
  Set OutRng = Intersect(OutRng, InputRng.Offset(1))
  OutRng.EntireRow.Interior.Color = vbYellow
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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