Consulting

Results 1 to 3 of 3

Thread: Find specific cell and insert page break - repeat till end of sheet

  1. #1
    VBAX Regular
    Joined
    May 2015
    Posts
    30
    Location

    Find specific cell and insert page break - repeat till end of sheet

    Hi,
    I have this code which is not ideal:

    Sub Find_cell_and_insert_HPageBreak ()
    Selection.Find(What:="Příloha  č.", After:=ActiveCell, LookIn:=xlFormulas _        , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Activate
    
    
    If ActiveCell.EntireRow.Hidden Then
    GoTo NEXT1
    Else
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
    End If
    NEXT1:
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Selection.Find(What:="Příloha  č.", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Activate
    
    
    If ActiveCell.EntireRow.Hidden Then
    GoTo NEXT2
    Else
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
    End If
    NEXT2:
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    End Sub


    I continue with this into something like NEXT50: ...Can someone please help me to make this more sophisticated (its also slow). Thank you

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    assuming the text to be found is in Column 1 / Column A

    Sub vbax_95347_insert_hpb_at_each_cell_with_specific_text()
    
    
        Dim i As Long
        Dim LastRow As Long
        
        With Worksheets("Sheet1") 'change Sheet1 to suit
            LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            
            For i = 1 To LastRow
                If .Cells(i, 1).Value Like "SearchText" Then 'change SearchText to suit
                    .HPageBreaks.Add Before:=.Rows(i)
                End If
            Next
        End With
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    Cunajz's code is dependent on the selection before it is run. So is this:
    Sub Find_cell_and_insert_HPageBreak()
    Dim rng As Range
    
    With Selection
      Set rng = .Find(What:="Příloha  č.", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
      If Not rng Is Nothing Then
        firstAddress = rng.Address
        Do
          If Not rng.EntireRow.Hidden Then ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=rng
          Set rng = .FindNext(rng)
        Loop While Not rng Is Nothing And rng.Address <> firstAddress
      End If
    End With
    End Sub
    Another way to avoid hidden cells is to get .Find to ignore them using LookIn:=xlValues instead of LookIn:=xlFormulas:
    Sub Find_cell_and_insert_HPageBreak()
    Dim rng As Range
    
    With Selection
      Set rng = .Find(What:="Príloha  c.", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
      If Not rng Is Nothing Then
        firstAddress = rng.Address
        Do
          ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=rng
          Set rng = .FindNext(rng)
        Loop While Not rng Is Nothing And rng.Address <> firstAddress
      End If
    End With
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •