+ Reply to Thread
Results 1 to 6 of 6

VBA/Macro to detect a heading at the bottom of a page and create a page break

  1. #1
    Registered User
    Join Date
    12-07-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    VBA/Macro to detect a heading at the bottom of a page and create a page break

    Hi everyone

    First up, sorry about the long post, I thought I would write down a bit of a background of how the code works etc. before I post my problem.
    I have created an Interview Guide Builder that will be used by people to choose from a list of questions and write some of their own to ask candidates who are applying for jobs.
    I have one macro within this document, titled “GenerateGuide”. It generates a pdf Interview Guide that the interviewer will take with them the meeting.

    Please Login or Register  to view this content.
    The way I have set it up to run is:
    - The user selects / writes the question and presses the “Generate Guide” button which starts the macro
    - All questions appear in a hidden sheet titled “Interview Guide” and have a formula in column B which decides whether the question has been selected or not.
    - The macro unhides the hidden “Interview Guide” worksheet and makes a copy of it. The original “Interview Guide” is then re-hidden.
    - It then filters columns B to only show those which were not selected. It then deletes these rows.
    - Column B is then deleted.
    - To create an individual title for each time the Guide is generated, the macro then creates the title which ends up in cell G2.
    - The guide is then published as a PDF (as I can’t copy and paste into word and keep the formatting nice).
    - The duplicate worksheet is then deleted. End macro.
    The macro itself is probably a bit clunky , but I’m a bit of a novice and so I’m happy with what it can do so far (if anyone has whiz bang ideas to make it run smoother, by all means let me know!!).

    What I am having trouble with is this… In the example I have attached, you will see that I have pre-selected some questions etc to help show my issue.
    When I run the macro and it publishes in pdf, you will notice some of the grey headings eg. “Managing Services” and “Leads and Empowers” sit at the bottom of two pages and the questions meant for that section are then on the next page. This will change depending on the questions selected by the user though, but happens quite frequently. What I want is that if a new heading comes up, then the first question under that heading appears straight after it (ie. On the same page) – does that make sense?
    I’ve tried resizing rows etc, but no luck.

    I have a few thoughts on how this could be fixed, but no idea how to do it:
    - As part of the macro, if it notices a heading at the bottom of a page, it creates a page break to push it over to the next page (probably preferred option).
    - A word document is created (keeping the formatting as it is set up in the excel file) and the user can press enter to push the headings down to the next page themselves if they notice it.
    Any ideas?

    Cheers
    Jess.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA/Macro to detect a heading at the bottom of a page and create a page break

    Hi jdodz

    I won't guarantee this Code will work universally but it appears to work on your Sample File.

    What it's doing is identifying Line Number of each Page Break in the Document. It then looks at each of these Page Break Rows...if the Cell above it (in Column B) has a Color Index of Grey(15), the Code inserts a Manual Page Break.

    I've also taken the liberty of streamlining the Code a bit
    if anyone has whiz bang ideas to make it run smoother, by all means let me know
    Try it...let me know of issues...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: VBA/Macro to detect a heading at the bottom of a page and create a page break

    I had already developed this code when I came back and saw that jaslake had replied, but I thought I might as well post it too. It goes through "Part 3" and adds page breaks as needed, based on the sum of the row sizes in each section.

    I also cleaned up the existing code as you had requested. The macro recorder puts in a lot of unneeded code. It all seemed to work when I ran it. Have fun!

    Please Login or Register  to view this content.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  4. #4
    Registered User
    Join Date
    12-07-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA/Macro to detect a heading at the bottom of a page and create a page break

    Hi John and Natefarm

    Both appear to work perfectly!
    I'm just getting my team to test it with me (so that we can get a bunch of combinations to check the headings are pushing down where needed), but thank you so much in advance!
    I'll let you know if there are any issues, but at this point, I'm happy to mark as solved.

    Cheers
    Jess.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA/Macro to detect a heading at the bottom of a page and create a page break

    You're welcome...glad I could help. Thanks for the Rep.

  6. #6
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: VBA/Macro to detect a heading at the bottom of a page and create a page break

    Dittos from me.

+ 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. Macro to create page break based on cells
    By matnelso in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2013, 05:48 PM
  2. Create insert page break macro
    By qtam82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2012, 03:57 AM
  3. Heading based on page break
    By scottydogg84 in forum Excel General
    Replies: 0
    Last Post: 09-28-2011, 10:26 AM
  4. Detect page break settings
    By JR_06062005 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-26-2006, 10:25 AM
  5. AUTO INSERT PAGE BREAK FOR EACH COPIED HEADING
    By wil4d in forum Excel General
    Replies: 4
    Last Post: 12-21-2005, 12:10 AM

Tags for this Thread

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