+ Reply to Thread
Results 1 to 7 of 7

Find row number above specific row, then insert page break

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-27-2009
    Location
    Dallas, TX
    MS-Off Ver
    Office 365
    Posts
    215

    Find row number above specific row, then insert page break

    I'm having a heck of a time figuring this out. So any help is GREATLY appreciated.

    I'm essentially trying to insert a page break on a row that will change from time to time. In this report, range A22:A150 is marked as either 0,1 or 2. 0 means the line is excluded from the report, 1 means the line is included and 2 means include but it is also the last line of a previous section of the report.

    If the report cuts off in the middle of a section, I need to find the row where the page break occurs (GOT THAT), then find the row with the previous "2" above that row. Then insert a horizontal page break below that line. I have tried all kinds of methods and I'm just getting nowhere. This is where I am at currently:

    Sub findpagebreak()
    
    
        Dim ws As Worksheet
    
    
        Set ws = Sheets("Quote Sheet")
    
    
        With ws.HPageBreaks
            If .Count > 0 Then
                MsgBox .Item(1).Location.Row
            Else
                MsgBox "No Page Breaks on this Sheet"
            End If
        End With
    
    End Sub
    Last edited by mvparker79; 03-31-2017 at 01:38 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Find row number above specific row, then insert page break

    Try something like this...


    Sub findpagebreak()
        
        Dim ws As Worksheet, rng2 As Range
        
        Set ws = ActiveSheet 'Sheets("Quote Sheet")
        
        With ws.HPageBreaks
            If .Count > 0 Then
                MsgBox .Item(1).Location.Row, , "Page break row"
                Set rng2 = ws.Range("A22:A150").Find(What:="2", _
                                               After:=ws.Range("A" & .Item(1).Location.Row), _
                                               LookIn:=xlValues, _
                                               LookAt:=xlWhole, _
                                               SearchOrder:=xlByRows, _
                                               SearchDirection:=xlPrevious, _
                                               MatchCase:=False)
                If Not rng2 Is Nothing Then
                    MsgBox rng2.Row, , "Previous '2' row"
                    .Add rng2.Offset(1)
                Else
                    MsgBox "No '2' found previous to the PageBreak."
                End If
                                          
            Else
                MsgBox "No Page Breaks on this Sheet"
            End If
        End With
        
    End Sub
    Last edited by AlphaFrog; 03-31-2017 at 01:29 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    04-27-2009
    Location
    Dallas, TX
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Find row number above specific row, then insert page break

    It tells me the page break row, but then I get a "Type Mismatch" error on the set rng2 line.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Find row number above specific row, then insert page break

    Quote Originally Posted by mvparker79 View Post
    It tells me the page break row, but then I get a "Type Mismatch" error on the set rng2 line.
    I edited the "A22:A150" range in the original post, try it again.

  5. #5
    Forum Contributor
    Join Date
    04-27-2009
    Location
    Dallas, TX
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Find row number above specific row, then insert page break

    I tried using this but I'm getting a "Type Mismatch" error on the If.cel.Find row.

    Sub findpagebreak()
    
        Dim ws As Worksheet
        Dim PB As Integer
        Dim cel As Range
        Dim rng As Range
        
        Set ws = Sheets("Quote Sheet")
    
        With ws.HPageBreaks
            If .Count > 0 Then
                MsgBox .Item(1).Location.Row
                PB = .Item(1).Location.Row
            Else
                MsgBox "No Page Breaks on this Sheet"
            End If
        End With
    
    Set rng = Range("A22:A" & PB)
    
    For Each cel In rng
        If cel.Find(2, Range("A22:A" & PB), searchdirection:=xlPrevious) = 2 Then
        cel.PageBreak = xlPageBreakManual
        Else: MsgBox "crap"
    End If
    
    Next cel
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    04-27-2009
    Location
    Dallas, TX
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Find row number above specific row, then insert page break

    Omg!!!! I could kiss you!!!!

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Find row number above specific row, then insert page break

    You're welcome. Glad it worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Insert page break
    By BRISBANEBOB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2014, 04:49 PM
  2. Find First match value and add page break, findnext value and add page break
    By dwx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2013, 12:58 PM
  3. Replies: 1
    Last Post: 01-28-2013, 02:14 AM
  4. Insert blank row, sum and insert page break macro
    By kim5012 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2012, 01:46 AM
  5. insert page header after page break
    By dgkindy in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-17-2009, 10:35 AM
  6. insert page break
    By ruben via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-11-2006, 07:00 AM
  7. find last entry and insert page break & line
    By littleme in forum Excel General
    Replies: 3
    Last Post: 03-30-2006, 05:15 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1