Help With Dynamically Adding Page Breaks to a Dynamic Worksheet for Printing

DrParmeJohnson

New Member
Joined
Feb 28, 2019
Messages
44
Hello,
I'm working on this sheet, which the snippet is only a piece of, which is the result of a macro which determines the numbers of items that should be placed into a box from each of our respective PO's. The issue I'm trying to resolve is that this sheet often will have one of the PO Names on the end of the sheet at the bottom of a page (as we print this out as a light guide) and then it's items will get kicked onto the next page.

So like the bottom of this sheet, 1GBH7KII - AVP1 would be the last line and then Calcium Chloride - 2 oz. : LGJY would get pushed onto page 2 and it just makes things confusing.

The issue then that I'm trying to figure out how to fix is how to make this macro figure out if not the entire PO is on the page, insert a page break and kick the whole thing to the next one, thus keeping it one unit. I know that each printed sheet after my page formatting is about 46 rows at the most. I also am not sure if there is a way to figure out like a "printed page index" so you can keep track of which page the info is on.

So, if anyone has any ideas on how this may be able to be achieved or some other solution that has the same effect, I would greatly appreciate it.

Thanks,
DPJ

PurchaseOrderItems (10).csv
ABCDEFGHIJK
1Number To Go In Appropriate Box45706312379039120364
2117M8W7P - MDW245706312379039120364
3Calcium Chloride - 2 oz. : LGJY
4Calcium Carbonate - 1 lb. : KHLQ4
5Potassium Metabisulfite - 2 oz. : JQ7C
638mm Metal Caps - 12 ct. : J2Y6
7Cider Yeast - 3 ct. : GUPW
8Briess Maltoferm DME - 1 lb. : GBXQ
9Gypsum- 1 lb. : GEZG7
10Campden Tablets SMS - 100 ct. : 0KC6
111C487V1F - MDW245706312379039120364
12DADY - 1 lb. : NG8A5
13Cider Yeast - 3 ct. : GUPW21
14Corn Sugar - 1 lb. : YDD4
15Corn Sugar - 4 lbs. : FVSW5
16Sparkolloid Powder - 1 lb. : ZQMG
17#9 x 1 1/2" First Quality Corks - 100 ct. : VZCG
181GBH7KII - AVP145706312379039120364
19Calcium Chloride - 2 oz. : LGJY
20Calcium Carbonate - 1 lb. : KHLQ61
Boxes for POs
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The following macro will first reset all page breaks, and then loop through each horizontal page break to make any necessary adjustments.

So, basically, for each horizontal page break, it checks whether the first item on the next page is a purchase order number. If not, it keeps checking the previous row until it finds one. Once it finds one, it moves the horizontal page break to that new location, and all subsequent horizontal page breaks are automatically re-calculated.

Note that when a single purchase order extends beyond one page, the horizontal page break will remain in place.

Also, note that the macro assumes that the sheet containing the data is the active sheet.

VBA Code:
Option Explicit

Sub Adjust_HPageBreaks()

    ActiveSheet.ResetAllPageBreaks
    
    Dim view_type As Long
    view_type = ActiveWindow.View 'remember current view
    
    ActiveWindow.View = xlPageBreakPreview 'required to set new location for HPageBreak
    
    Dim hpagebreak_index As Long
    Dim current_location As Range
    Dim current_item As String
    
    hpagebreak_index = 1
    Do While hpagebreak_index <= ActiveSheet.HPageBreaks.Count
        Set current_location = ActiveSheet.HPageBreaks(hpagebreak_index).Location
        current_item = Replace(current_location.Value, " ", "")
        Do While (Not current_item Like "????????-????")
            Set current_location = current_location.Offset(-1, 0)
            current_item = Replace(current_location.Value, " ", "")
        Loop
        If current_location.Row > 2 Then
            Set ActiveSheet.HPageBreaks(hpagebreak_index).Location = current_location
        End If
        hpagebreak_index = hpagebreak_index + 1
    Loop
    
    ActiveWindow.View = view_type 'restore original view
    
End Sub

Hope this helps!
 
Upvote 0
Changed it to reflect it being worksheet 3 (ws3) but otherwise works great. Only other weird thing is that my print preview won't load nor connect to my printer but I think that may be a separate issue.

Thank you again, I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top