• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel VBA conditional insert of page breaks

mrwad

New Member
I have a form that is changing all the time according to my "Filling form" where user is filling in information. Then I have "Print version" sheet for autoformatting and printing to .pdf/.xls. In "Print version" I have paragraphs of text in column "C". Some cells with text of column "C" are too long so I am wrapping them with my VBA (.WrapText = True). I want to make conditional page breaks that will read through my Print Area and insert page breaks after each empty row after each paragraph that is not fitting to page compleatly. My VBA code below is working fine except for text being wrapped. If I remove all cells with "Wrap text" command each row have some constant height, let's say 15 so I know amount of rows could be fitted on the page and set my "PgSize = 91" or whatever it is but if I wrap text I don't know how many rows can be fitted on the page. So the problem is: If I set "PgSize = 91" in "Sub FitGroupsToPage()" (that's an amount of rows could be fitted to each page) to 91 and don't wrap my text then everything works fine. However text must be wrapped to fit to my page vertically. Then there is not 91 rows but less, depending on the length of the text in wrapped cells. So number 91 is dynamic each time after hiding and wrapping "Sub FitMyTextPlease()" and "Sub HideMyEmptyRows()" and "Sub SetPrintArea()". Number of rows can also be different on every page (depending of how much text there are in wrapped cells on each page). Any ideas of how this issue can be fixed or maybe suggest some other way of approaching this?


Code:
Sub FitMyTextPlease()
  Application.ScreenUpdating = False
   ThisWorkbook.Sheets("Print version").PageSetup.CenterHeader = "&""Times New Roman,Bold""&12 " & Range("Data!V28").Text & Chr(13) & Chr(13) & " " & "&""Times New Roman,Normal""&12 " & Range("Data!V30").Text
   
   'ThisWorkbook.Sheets("Print version").PageSetup.CenterHeader = Range("Data!V28").Text


   ThisWorkbook.Sheets("Print version").Select
   With ActiveWorkbook.ActiveSheet
           With .Cells.Rows
               .WrapText = True
               .VerticalAlignment = xlCenter
               .EntireRow.AutoFit
           End With '.Cells.Rows
           .Columns.EntireColumn.AutoFit
       End With 'sheet
       Application.ScreenUpdating = True
End Sub
Sub HideMyEmptyRows()
   Dim myRange As Range
   Dim cell As Range
   Application.ScreenUpdating = False
   Set myRange = ThisWorkbook.Sheets("Print version").Range("Print_Area")
       For Each cell In myRange
       myRange.Interior.ColorIndex = 0
       If cell.HasFormula = True And cell.value = "" And cell.EntireRow.Hidden = False Then Rows(cell.Row).EntireRow.Hidden = True
   Next
   Application.ScreenUpdating = True
End Sub
Sub SetPrintArea()
 Dim ws As Worksheet
 Dim lastRow As Long


 Set ws = ThisWorkbook.Sheets("Print version")


 ' find the last row with formatting, to be included in print range
 lastRow = ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row


 ws.PageSetup.PrintArea = ws.Range("A1:C" & lastRow).Address
End Sub
Sub HowManyPagesBreaks22()
   Dim iHpBreaks As Integer, iVBreaks As Integer
   Dim iTotPages As Integer


   iHpBreaks = ActiveSheet.HPageBreaks.Count + 1
   iVBreaks = ActiveSheet.VPageBreaks.Count + 1


   iTotPages = iHpBreaks * iVBreaks
   MsgBox "This sheet will require " & iTotPages & _
   " page(s) to print", vbInformation, "Pages counted"
End Sub
Sub Printed_Pages_Count()
   
   Range("A1").value = (ActiveSheet.HPageBreaks.Count + 1) * (ActiveSheet.VPageBreaks.Count + 1)
   
End Sub
Sub HowManyPagesBreaks()


   MsgBox ExecuteExcel4Macro("Get.Document(50)")


End Sub
Sub FitGroupsToPage()
   Dim rStart As Range, rEnd As Range, TestCell As Range
   Dim lastRow As Long, PgSize As Integer
   Dim n As Integer
   
   PgSize = 91   '  Assumes 91 rows per page
   Set rStart = Range("C1")
   lastRow = Cells(Rows.Count, 1).End(xlUp).Row
   
   Do
       Set TestCell = rStart.Offset(PgSize, 0)
       If Len(TestCell) = 0 Or Len(TestCell.Offset(-1, 0)) = 0 Then
               Set rEnd = TestCell.End(xlUp)
           Else
               Set rEnd = TestCell.End(xlUp).End(xlUp)
       End If
       ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=rEnd.Offset(1, 0)
       Set rStart = rEnd.Offset(1, 0)
   
   n = n + 1
   If n > 1000 Then Exit Sub   '  Escapes from an infinite loop if code fails
   Loop Until rStart.Row > lastRow - 50
End Sub
Sub FitMyHeadings()
Call FitMyTextPlease
Call HideMyEmptyRows
Call SetPrintArea
Call FitGroupsToPage
Call Printed_Pages_Count
End Sub
 
mrwad
I want to make conditional page breaks that will read through my Print Area and insert page breaks after each empty row after each paragraph that is not fitting to page compleatly.
Which are Your paragraphs? How to find/regonize those?
... or are those ... hmm? ... every tenths row?
 
Problem is that paragraphs are not every 10th or 15th or 18th row. There are can be different amount of paragraphs and rows in each paragraph. They always have a "heading" so maybe it can help somehow. Bold text with heading and then paragraph itself. This complete "block" should be on one page and if it doesn't fit to this page then VBA code should move it to the next page.
 

Attachments

  • PageBreakIssue.jpg
    PageBreakIssue.jpg
    223.8 KB · Views: 13
  • ParagraphDescription.jpg
    ParagraphDescription.jpg
    279.2 KB · Views: 19
mrwad
... no matter of row count!
1) Bold text ... there are bolded texts ... no help, neither 'maybe'!
eg Work history ... hmm? Which are 'blocks'?
2) Those 'block' which should keep in one page have to know!
... and if the whole block cannot keep ... then it have to cut!
3) There are many empty rows. Do those need to be there?
If need then okay... otherwise?
4) After You can figure even for Yourself more clear rules,
then there could set page breaks by code.
 
mrwad
... no matter of row count!
1) Bold text ... there are bolded texts ... no help, neither 'maybe'!
eg Work history ... hmm? Which are 'blocks'?
2) Those 'block' which should keep in one page have to know!
... and if the whole block cannot keep ... then it have to cut!
3) There are many empty rows. Do those need to be there?
If need then okay... otherwise?
4) After You can figure even for Yourself more clear rules,
then there could set page breaks by code.

I have already came up with almost working code. Please see this message attachment. It works if you don't wrap the text on the page.
Code:
.WrapText = False
However if you set
Code:
.WrapText = True
in first Macro. It is not working as
Code:
PgSize = 91
is not 91 anymore.
 

Attachments

  • CV_form- Copy.xlsm
    90.8 KB · Views: 0
mrwad ... It works if you don't wrap the text on the page.
So, didn't You want it work also with 'wrap'?

Did You tried to read my #5 replies?
You should know which part have to try to keep in same page!

No matter of rows!
If 'part' would be longer than page,
then that 'part' have to split to two pages!
 
I can use "START" and "END" in Column "A" to separate part (start and end of my part). I can use formula to do such a thing. Will it help anyhow?
 

Attachments

  • ParagraphDescription.jpg
    ParagraphDescription.jpg
    275.8 KB · Views: 5
What are rows between 'end' and 'start'?
If You can use formulas to do such a thing
... then those text won't need to be in A-column.
 
Well. I can just add at this stage START and END manually. Later I will develop some formula to get these texts in needed places. See attachment.
 

Attachments

  • CV_form- Copy.xlsm
    90.9 KB · Views: 4
... well ... yes! ...
1) If I would start to think to make any code
then there won't be comment 'Later...'
In many cases 'Later...' means that after 'later'
someone should start almost from zero with code!

2) If there are already code ...
Screen Shot 2018-10-19 at 12.04.36.png
which prevents to use and test file
with my Excel version
then it will be also a challenge!
 
2) This Macro is not necessary so it can be deleted completely.
 

Attachments

  • CV_form- Copy.xlsm
    88.9 KB · Views: 2
Back
Top