[RESOLVED] Excel VBA - Print only "not empty" pages
Excel 2007, VBA
I need to print only pages with some text on it.
Blank pages are random and they exsist because i put page break each one row or each two rows in that sheet (i need to print one or two rows on each sheet). In sheet these blanke pages are hidden rows but because page break they printout blank.
Can i make loop and test condition something like this?:
For each page in pages (printpreview)
If page (printpreview) > 0 letters then
printout
End If
Next page
i don't think you can do it as easy as that, but you can test like this, this only test the first page, so you would have to loop through all the pagebreaks to test each range, then print each range
vb Code:
Set s = ThisWorkbook.Sheets("sheet2")
With s
vb = .VPageBreaks(1).Location.Column - 1
hb = .HPageBreaks(1).Location.Row - 1
Set r = .Range(.Cells(1, 1), .Cells(hb, vb))
For Each c In r
If Not IsEmpty(c) Then notempty = True: Exit For
Next
If notempty Then r.PrintOut
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Thanks for help Westconn1 but that didn't work
I still get empty pages out.
Here is attach with this workbook and can you please look at it and see whats wrong?
In this workbook I cut out 4/5 of TOC (in bouth sheets) for easyer working (somethimes is on sheet "Naslovi" one row per page somethimes are two rows per page, in original file)
Sheet pass = " " 'space
In normal work, one can edit only blue fields in sheet "Sadrzaj", edit "1" and "0" on left side, btn "Skrati", than print first sheet, than second sheet (rows "marked" with "0" in first sheet are not suposed to printout!).
btn "Skrati" - hide rows in both sheets that have "0" on column A.
btn "Produzi" - unhide rows in both sheets.
btn "Printaj" - your VBA code
when i wrote the loop i forgot to rest the boolean, should be
vb Code:
If notempty Then r.PrintOut
notempty = false 'so that next page range is empty till it checks data
i can not open your workbook
Last edited by westconn1; Oct 26th, 2007 at 04:48 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
lol, that is why i could not open it
as you have a print area set, you need to use that to get all your print ranges, also your hidden cells are not empty as they have formulas in them
try like this
vb Code:
Sub Printaj_Naslove()
Application.ScreenUpdating = False
Call UnLockS
Dim s As Worksheet
Dim r As Range, p As Range, vs As Integer, vb As Integer, vp As Integer
Set p = .Range(.PageSetup.PrintArea) ' set the print area as a range, so we can find the edges
vs = p.Range("a1").Row
hs = p.Range("A1").Column
vp = .VPageBreaks.Count
hp = .HPageBreaks.Count
For i = 1 To vp + 1
For j = 1 To hp + 1
If i = vp + 1 Then
vb = p.Range("a1").Column + p.Columns.Count - 1
Else
vb = .VPageBreaks(i).Location.Column - 1
End If
If j = hp + 1 Then
hb = p.Range("a1").Row + p.Rows.Count - 1
Else
hb = .HPageBreaks(j).Location.Row - 1
End If
Set r = .Range(.Cells(hs, vs), .Cells(hb, vb))
Debug.Print r.Address;
' For Each c In r
' If Not IsEmpty(c) Then notempty = True: Exit For
' Next
' If notempty Then Debug.Print , r.Address; ' r.PrintOut
If r.EntireRow.Hidden = False Then Debug.Print , r.Address;: r.PrintOut
notempty = False
Debug.Print
hs = hb + 1
Next j
hs = 1
vs = vb + 1
Next i
End With
Sheets("Sadrzaj").Select
Range("B1").Select
Call LockS
Application.ScreenUpdating = True
End Sub
you can take out the debug prints, i was just using that to test the ranges being returned, which are as below, showing the ranges returned and the ranges printed
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Thanks for helping me so much, it worked partialy
Today, I lerned that i can make "debug print" to immidiate window and see result before printing (saves some paper).
But the problem is that in Debug print i get all corect pages, but on printer i get only even pages out (you can test that if you printout those pages on paper or on some kind of pdf printer - adobe pdf printer, pdf995 etc.)
I tried to make corections in your code but that did'nt produce results.
well at least that is a start, i have no idea why it would do like that, check your page/ printer setup etc, i will look at it later
i did a test run, my pdf printer did 21 pages, which was the same as the number of range printed to the debug window
Last edited by westconn1; Oct 27th, 2007 at 04:32 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
This is a better procedure, it use SUBTOTAL function 103 (COUNTA) to count non-blank cells in each page, instead of checking hidden rows and empty cells. You don't need to Unlock then Lock the sheet.
I recommend you to add Option Explicit to the top of the module and declare all variables used.
Code:
Sub Printaj_Naslove()
Dim ws As Worksheet
Dim prRange As Range
Dim vp As Long, hp As Long
Dim i As Long, j As Long
Dim c1 As Long, c2 As Long
Dim r1 As Long, r2 As Long
Dim n As Long
Dim BreakRows() As Long
Dim BreakCols() As Long
Dim PageRange As Range
Set ws = ThisWorkbook.Sheets("Naslovi")
With ws
vp = .VPageBreaks.Count
hp = .HPageBreaks.Count
ReDim BreakCols(0 To vp + 1)
ReDim BreakRows(0 To hp + 1)
Set prRange = .Range(.PageSetup.PrintArea)
BreakCols(0) = prRange.Column
For i = 1 To vp
BreakCols(i) = .VPageBreaks(i).Location.Column
Next
BreakCols(vp + 1) = BreakCols(0) + prRange.Columns.Count
BreakRows(0) = prRange.Row
For j = 1 To hp
BreakRows(j) = .HPageBreaks(j).Location.Row
Next
BreakRows(hp + 1) = BreakRows(0) + prRange.Rows.Count
For i = 0 To vp
c1 = BreakCols(i)
c2 = BreakCols(i + 1) - 1
For j = 0 To hp
r1 = BreakRows(j)
r2 = BreakRows(j + 1) - 1
Set PageRange = .Range(.Cells(r1, c1), .Cells(r2, c2))
n = WorksheetFunction.Subtotal(103, PageRange) '-- Count non-blank cells
If n > 0 Then
Debug.Print PageRange.Address
'PageRange.PrintOut
End If
Next
Next
End With
End Sub