+ Reply to Thread
Results 1 to 16 of 16

Page Breaks when value changes

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Page Breaks when value changes

    Then this should do the trick

    Sub SetHPageBreaks()
        
        Dim LastDataRow As Long
        Dim lngRow As Long              'Counter to loop through each row
        Dim EvaluatingData As String
        Dim SheetName As String
        
        SheetName = ThisWorkbook.Worksheets(1).Name                             'Sheet name where the data is located.
        LastDataRow = Worksheets(SheetName).Cells(Rows.Count, 2).End(xlUp).Row  'This is the row number of the last cell with data in Column B
        EvaluatingData = Worksheets(SheetName).Cells(LastDataRow, 2).Value      'Value inside each cell being evaluated during the loop.
        
        Worksheets(SheetName).ResetAllPageBreaks  'This line will errase all the custom Pagebreaks.  if you have custom pagebreaks that you need to keep, then delete.
        
        For lngRow = 1 To LastDataRow  'Modify the 1 with the row number for the first data value.
            
            If Worksheets(SheetName).Cells(lngRow, 2).Value <> EvaluatingData And Worksheets(SheetName).Cells(lngRow, 2).Value <> "" Then
                Worksheets(SheetName).HPageBreaks.Add Before:=Rows(Worksheets(SheetName).Cells(lngRow, 2).Row)
                EvaluatingData = Worksheets(SheetName).Cells(lngRow, 2).Value
                
            End If
        
        Next
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Page Breaks when value changes

    Almost forgot; to make the procedure run on every data change in column B, copy the following procedure inside the code page where you have the data:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 2 Then
            SetHPageBreaks
        End If
        
    End Sub
    Remember to keep the SetHPageBreaks inside a Module.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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