Results 1 to 4 of 4

Finding page breaks in excel sheets & inserting empty row

Threaded View

  1. #1
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Finding page breaks in excel sheets & inserting empty row

    Hi all,

    I am trying to find a way to identify which page a cell is on (after a print preview is performed) and then insert rows to the worksheet based on what values are found before and after the page break:

    if values on page1 <> values on page2 then just add header:

    1
    1
    1
    1
    ----page break----
    2
    2
    2
    3
    3

    becomes:

    1
    1
    1
    1
    ----page break----
    <insert row>
    2
    2
    2
    3
    3

    and if values page1 = values page2 then insert rows until they don't

    1
    1
    1
    1
    2
    2
    -------page break-----
    2
    2
    3
    3
    3

    becomes

    1
    1
    1
    1
    <insert row>
    <insert row>
    ----page break----
    <insert row>
    2
    2
    2
    2
    3
    3
    3


    The code I have so far is:

    Private Sub setPageHeaders()
    
    Dim lrow, x, page1, page2, i As Integer
    Dim rowSelection As String
    
        lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        
    For x = 2 To lrow
        page2 = #getPageNumber(ActiveSheet.cells(x, 1))#         'get the page number of current line
        page1 = #getPageNumber(ActiveSheet.cells(x - 1, 1))#    'get the page number of the previous line
        
        If page2 <> page1 Then                                               'compare page numbers until the do not match, indicating page break
            
            If ActiveSheet.Cells(x, 1).Value = ActiveSheet.Cells(x - 1, 1).Value Then   'if values do not match, insert header row at top of page
                ActiveSheet.Rows(x).Select
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            
            ElseIf ActiveSheet.Cells(x, 1).Value = ActiveSheet.Cells(x - 1, 1).Value Then   'find if values do match, find value that does not match and move down to next page & add header row
                i = 0
                
                Do Until ActiveSheet.Cells(x, 1).Value <> ActiveSheet.Cells(x - i, 1).Value 'when the do match, find last value on page1 that matches
                    i = i + 1
                Loop
                
                rowSelection = x - i & ":" & x                                                'insert rows so that there is a clean break between values
                ActiveSheet.Rows(rowSelection).Select
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        End If
    Next x
    where the two lines with the #getPageNumber()# variable standin is where I don't know the command for getting the page number.

    Is there an easy way to get this?
    Last edited by Ouka; 04-09-2015 at 08:38 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Inserting & Removing Page Breaks
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-12-2014, 02:06 PM
  2. Removing Existing Page Breaks and Inserting a Page Break every third Total Line
    By Kalvas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-17-2013, 05:12 AM
  3. Batch Inserting Row Sheets in between page breaks
    By TeraFractal in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2008, 06:40 PM
  4. Inserting Page Breaks
    By chrisnelsonusa1 in forum Excel General
    Replies: 3
    Last Post: 12-09-2005, 05:40 PM

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