+ Reply to Thread
Results 1 to 4 of 4

Macro for page break in columns not working

  1. #1
    Registered User
    Join Date
    05-24-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    29

    Macro for page break in columns not working

    I'd like to put a page break in the attached worksheet. I have the below macro that I thought I used last time, but when I run it now, it puts a page break after changes in all the data and I ended up getting over 900 pages on my 500+ line spreadsheet.

    What I would like to do, is after each "total" in column B, have that be the end of the page and have a new page start. In the attached example sheet, I would like page 2 to start on line 26, and page 3 to start on line 38.

    This is my current macro, hopefully I can just modify it a bit.

    ******************************************************************************************
    Sub AddPageBreaks()

    Dim R As Long
    Dim Rng As Range
    Dim RngEnd As Range
    Dim Wks As Worksheet


    Set Wks = ActiveSheet

    Set Rng = Wks.Range("B2")
    Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)

    If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)

    For R = 1 To Rng.Rows.Count - 1
    If Rng.Item(R, 1) <> Rng.Item(R + 1, 1) Then
    Rng.Item(R + 1, 1).PageBreak = xlPageBreakManual
    End If
    Next R

    End Sub
    ******************************************************************************************

    Report - Change in Column Value.xls

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro for page break in columns not working

    Hi, dlorenzo,

    please use Code-Tags for displaying the information.

    Maybe try this code:
    Please Login or Register  to view this content.
    The updated version of your code could deliver what you want:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    05-24-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Macro for page break in columns not working

    Thank you Holger,

    I appreciate you responding, however, I still end up getting page breaks after each change in column B (not just after Total Account) and then page breaks between columns (A and B) as well as columns (D and E).

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro for page break in columns not working

    Hi, dlorenzo,

    silly question from my side: did you remove all pagebreaks prior to running the first macro?

    Ciao,
    Holger

+ 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