Find nth .FindNext item

STEVEMILLS04

Board Regular
Joined
Oct 8, 2009
Messages
113
I currently use .Find to find the first instance of a value then use .FindNext to find the second instance. It works great because I use it to insert a page break after the second instance.

Now I need it to find the 10th instance of a value. If there are less than 10 the pagebreak will be at the end anyway, if there are more than 10 it should add it after the every 10th. Below is my code for every other (I am sure it can be much improved but I am no expert and it works!) but I don't know the best way to accomplish my goal for every 10th item.

Any suggestions? Also, does .Find and .FindNext only work on unhidden columns?

Code:
Sub insertPageBreaks()
    Dim oRange As Range, allRange As Range
    Dim ws As Worksheet, SearchString As String, firstFind As String, secondFind As String
          
    Set ws = ActiveSheet
    Set oRange = ws.Range("A:A")
      
    SearchString = "Total"
    
    Application.ScreenUpdating = False
    
    Columns("A").Hidden = False
    
    lastrow = ActiveSheet.UsedRange.Rows.Count
    lastRange = Range("A" & lastrow)
    
    With ws
       Set aCell = .Range("A:A").Find(What:="Total", After:=.Range("A1"), LookIn:=xlValues, LookAt:=xlPart)
    End With
    
    
            
    If Not aCell Is Nothing Then
        'ActiveWindow.View = xlPageBreakPreview
        ActiveSheet.ResetAllPageBreaks
        

        firstFind = aCell.Address
        If Range("rgnVisibility").Value = "Visible" Then
            Do
            
                If Not aCell Is Nothing Then
                    Set bCell = ws.Range("A:A").FindNext(After:=aCell)
                    
                    If bCell.Address = firstFind Then
                        Exit Do
                    End If
                
                    secondFind = bCell.Address
    
                    With ws
                        .HPageBreaks.Add .Range("propStart")
                        .HPageBreaks.Add .Range(bCell.Address).Offset(2, 0)
                    End With
    
                    Set aCell = ws.Range("A:A").FindNext(After:=bCell)
                    
                    If aCell.Address = firstFind Then
                        Exit Do
                    End If
                Else
                    Exit Do
                End If
            Loop
        End If
    End If
    
    Columns("A").Hidden = True
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
STEVEMILLS04,

To find the 10th item, you can count the number of iterations. For example...

Code:
Dim kount As Long
Do
    kount = kount + 1
    If kount = 10 Then
        MsgBox "Kount is " & kount
        Exit Do
    End If
Loop

And does .Find and .FindNext only work on unhidden columns? That's what my quick testing indicates. To get around this you can sandwich the .Find statement between .Hidden statements...

Code:
Columns(3).Hidden = False
Set r = Range("A:E").Find(What:="Sunday", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart)
Columns(3).Hidden = True

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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