PC Review


Reply
Thread Tools Rate Thread

Adding page breaks conditionally

 
 
RITCHI
Guest
Posts: n/a
 
      18th Jun 2008
Hi
I'm trying to insert a page break after a certain number (26) of
cells, with length >=21, is exceeded
I've grabbed snippets of code from here and there but can't get it
to
work.
Any help would be appreciated

Thanks
Ritchi


Sub InsertPageBreak()
'insert a page break after the count of cells in column 1 with a
defined length (>=21 by default) is exceeded (26 is the default count
to trigger a page insert)


Application.ScreenUpdating = False
ActiveSheet.Activate


Dim CountOfItems As Long
CountOfItems = 0


Call PageBreaksHorizontalRemove


lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 6 Step -1
'For i = 6 To lr Step 1
If Len(Cells(i, 1)) >= 21 Then CountOfItems = CountOfItems +
1
If CountOfItems = 26 Then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
If CountOfItems = 26 Then CountOfItems = 0
Next


Application.ScreenUpdating = True
End Sub


Sub PageBreaksHorizontalRemove()
'Remove all horizontal pagebreaks in active sheet
Dim pb As HPageBreak
Dim lCount As Long


For lCount = ActiveSheet.HPageBreaks.Count To 1 Step -1
Set pb = ActiveSheet.HPageBreaks(lCount)
If pb.Type = xlPageBreakManual Then pb.Delete
Next lCount


End Sub


 
Reply With Quote
 
 
 
 
TomPl
Guest
Posts: n/a
 
      20th Jun 2008
This code worked for me:

Sub InsertPageBreak()

Dim CountOfItems As Long
Dim LastRow As Long
Dim CurrentRow As Long

CountOfItems = 0
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For CurrentRow = 2 To LastRow
If Len(Cells(CurrentRow, 1)) >= 21 Then
CountOfItems = CountOfItems + 1
End If
If CountOfItems = 26 Then
ActiveSheet.HPageBreaks.Add before:=Cells(CurrentRow + 1, 1)
CountOfItems = 0
End If
Next CurrentRow

End Sub

"RITCHI" wrote:

> Hi
> I'm trying to insert a page break after a certain number (26) of
> cells, with length >=21, is exceeded
> I've grabbed snippets of code from here and there but can't get it
> to
> work.
> Any help would be appreciated
>
> Thanks
> Ritchi
>
>
> Sub InsertPageBreak()
> 'insert a page break after the count of cells in column 1 with a
> defined length (>=21 by default) is exceeded (26 is the default count
> to trigger a page insert)
>
>
> Application.ScreenUpdating = False
> ActiveSheet.Activate
>
>
> Dim CountOfItems As Long
> CountOfItems = 0
>
>
> Call PageBreaksHorizontalRemove
>
>
> lr = Cells(Rows.Count, 1).End(xlUp).Row
> For i = lr To 6 Step -1
> 'For i = 6 To lr Step 1
> If Len(Cells(i, 1)) >= 21 Then CountOfItems = CountOfItems +
> 1
> If CountOfItems = 26 Then
> ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
> If CountOfItems = 26 Then CountOfItems = 0
> Next
>
>
> Application.ScreenUpdating = True
> End Sub
>
>
> Sub PageBreaksHorizontalRemove()
> 'Remove all horizontal pagebreaks in active sheet
> Dim pb As HPageBreak
> Dim lCount As Long
>
>
> For lCount = ActiveSheet.HPageBreaks.Count To 1 Step -1
> Set pb = ActiveSheet.HPageBreaks(lCount)
> If pb.Type = xlPageBreakManual Then pb.Delete
> Next lCount
>
>
> End Sub
>
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2003 adding page breaks Gary Microsoft Excel Misc 2 13th Oct 2008 02:49 PM
Adding / Removing Page Breaks Michelle M Microsoft Access Reports 5 19th Nov 2005 05:54 AM
Re: How do I conditionally insert page breaks in a sorted list keyed . Gord Dibben Microsoft Excel Setup 2 23rd Sep 2004 12:52 AM
Re: How do I conditionally insert page breaks in a sorted list keyed . Dave Peterson Microsoft Excel Setup 0 22nd Sep 2004 12:44 AM
How do I conditionally insert page breaks in a sorted list keyed . =?Utf-8?B?Um9sZiBIYXJnaXM=?= Microsoft Excel Setup 0 21st Sep 2004 09:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 AM.