Consulting

Results 1 to 5 of 5

Thread: VBA Code - Insert Pagebreak in Selected Cells (Range)

  1. #1

    VBA Code - Insert Pagebreak in Selected Cells (Range)

    Hi!

    I'm trying to use this code to a range of cells selected. Any input will be appreciated


    Sub InsertPageBreaks()
    'Updateby20140618
    Dim xLastrow As Long
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    xRow = Application.InputBox("Row", xTitleId, "", Type:=1)
    xLastrow = xWs.Range("A15").SpecialCells(xlCellTypeLastCell).Row
    For i = xRow + 1 To xLastrow Step xRow
        xWs.HPageBreaks.Add Before:=xWs.Cells(i, 1)
    Next i
    End Sub
    Last edited by Paul_Hossler; 02-24-2017 at 04:55 PM. Reason: Added [CODE] tags - please use the [#] icon next time

  2. #2
    VBAX Newbie dangelor's Avatar
    Joined
    Aug 2014
    Location
    Indiana USA
    Posts
    4
    Location
    Don't quite know what it is you're asking. Your code (with a few subtle corrections) will insert a page break every xRow count of rows. Is that what you want to do?

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    
    Sub test()
        Dim xWs As Worksheet
        Dim sel As Range
        Dim xLastrow As Long
        Dim xRow As Long
        Dim i As Long
        
        Set xWs = ActiveSheet
        Set sel = Selection
      
        xRow = Application.InputBox("Row", Type:=1)
        If xRow < 1 Then Exit Sub
        
        xLastrow = sel.Cells(sel.Count).Row
        
        xWs.ResetAllPageBreaks
        
        For i = xRow + sel.Row To xLastrow Step xRow
            xWs.HPageBreaks.Add Before:=xWs.Cells(i, 1)
        Next i
        
    End Sub

  4. #4
    yes! However not to the entire sheet, only add page breaks to the selected range of cells. Rework.jpg
    For example, in reference to the attached sheet, selecting A15:K27.

  5. #5

    Cool

    Quote Originally Posted by mana View Post
    Option Explicit
    
    
    Sub test()
        Dim xWs As Worksheet
        Dim sel As Range
        Dim xLastrow As Long
        Dim xRow As Long
        Dim i As Long
        
        Set xWs = ActiveSheet
        Set sel = Selection
      
        xRow = Application.InputBox("Row", Type:=1)
        If xRow < 1 Then Exit Sub
        
        xLastrow = sel.Cells(sel.Count).Row
        
        xWs.ResetAllPageBreaks
        
        For i = xRow + sel.Row To xLastrow Step xRow
            xWs.HPageBreaks.Add Before:=xWs.Cells(i, 1)
        Next i
        
    End Sub

    You are the best.

    Thank you so much. I worked great. I removed the resetallpagebreak command and it did the trick.

Posting Permissions

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