VBA to insert page breaks at a blank with varying blck size

Simon B

New Member
Joined
Mar 22, 2013
Messages
10
Hi
I've written a macro that takes a lot of data and summarises it into blocks of data one page wide, and multiple pages long. Each block of data in the output is always the same length, but depending on where I extract the data from the length may be different in each run through, so depending on this each block may be 5 or 10 or 15 rows, in fact any number. Each block is separated by a blank cell in column B. What I want to do is put a page break in at the blank cell above where the 'natural' page break is, so that the blocks of data are not split over 2 pages. I have tried to write this and had a look on forum sites and have so far been unsuccessful, so any help anyone has would be greatly appreciated.
Thanks
Simon
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Can you post the part of your code that transposes the data onto the sheet please?
 
Upvote 0
I'm no Excel VBA expert, so the code that does this is very long, and not very pretty to be honest, but it takes data that is pages and pages long and summarises it. Not pretty, but all works, and the code wouldn't make sense without the original txt file that it pulls the data.
This last bit is a separate issue and a stand alone bit of code would hopefully just insert to the end of the macros.
I basically have 6 columns. A is a sample name. B-G are results, but there maybe more than 5 results, so it effectively wraps it underneath. Like below. This is only for 3 samples, and a few elements though, it could be many more samples and elements analysed, hence why I I would like to put a page break in at a blank point that is above the natural page break, not every blank.

Ba2304Ba2304-2Ba2335Ba2335-2Ba4130
ppmppmppmppmppm
Wash-0.0268-0.0269-0.0269-0.0265-0.0296
B117.5417.5417.5417.4617.69
B22.0191.9791.9951.9862.022
Ba4130-2S_1807S_1807-2S_1820S_1820-2
ppmppmppmppmppm
Wash-0.0515-84.63-81.02-82.18-80.59
B117.625.9226.2325.625.55
B21.9363.5331.4383.1251.127
S_1826S_1826-2Sr3380Sr3380-2Sr3464
ppmppmppmppmppm
Wash-79.82-80.1-0.0011-0.0004-0.0013
B125.6125.6617.617.3617.51
B23.3151.3661.9861.9361.995
Sr3464-2
ppm
Wash-0.0007
B117.52
B21.945

<colgroup><col><col span="5"></colgroup><tbody>
</tbody>
 
Upvote 0
So essentially you want a piece of code to look at each row and if it is empty in it's entirety, insert a page break?
 
Upvote 0
Yes and no. I want a page break on a blank row, but not every blank row, only the blank row that is above a 'natural' page break, so there may be multiple blocks of data with blank rows separating them, but if the 'natural' page break is in the middle of a block I want to go up from there to the first blank row above and put in a page break. Then continue down and do it all again for the whole list which may be several pages long. So depending on the original data the number of blocks that appears on a printed sheet may vary but they will always be full blocks. Hope that makes sense.
 
Upvote 0
Hi

Try this :-
Code:
Dim Pdiv, PgCt, Pgs
PgCt = 1
Pgs = ActiveSheet.HPageBreaks.Count
Do
    If Pgs = 0 Then
        Exit Do
    End If
    
    Pdiv = ActiveSheet.HPageBreaks(PgCt).Location.Row
    
    If Not IsEmpty(Range("B" & Pdiv).Value) Then
'   Cell is not empty therefore find the first occurrence of blank in Col B above this row
        Do
'          Loopback until there is an empty cell in Col B
            Pdiv = Pdiv - 1
        Loop Until IsEmpty(Range("B" & Pdiv))
'   Set the new Page break above the empty cell
    Set ActiveSheet.HPageBreaks(PgCt).Location = Range("B" & Pdiv)
    End If
    PgCt = PgCt + 1
Pgs = ActiveSheet.HPageBreaks.Count
Loop Until PgCt > ActiveSheet.HPageBreaks.Count

hth
 
Upvote 0
Hi Mike
This looks just the thing, but it's throwing up an error at:
Set ActiveSheet.HPageBreaks(PgCt).Location = Range("B" & Pdiv)

Thanks
Simon
 
Upvote 0
Simon

Take a note of variables Pdiv, PgCt and Pgs and put a break on that line of code.

By looking at your Page Break Preview you should be able to identify what is causing the error.

I have to go out for a while so there could be a delay responding if you can't solve the error.
 
Upvote 0
Hi Mike
Yeah, I watched all the variables, and stepped through. All the variables were fine but it still fell over.
I changed the line to:
Range("B" & Pdiv + 1).Select
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
and all works, so not really sure what the problem was. Thanks very much for your help, it's help me finish off the whole macro to give a nice user friendly output.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

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