Results 1 to 11 of 11

Thread: [RESOLVED] Excel VBA - Print only "not empty" pages

  1. #1

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Print only "not empty" pages

    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:
    1. Set s = ThisWorkbook.Sheets("sheet2")
    2. With s
    3. vb = .VPageBreaks(1).Location.Column - 1
    4. hb = .HPageBreaks(1).Location.Row - 1
    5. Set r = .Range(.Cells(1, 1), .Cells(hb, vb))
    6. For Each c In r
    7.     If Not IsEmpty(c) Then notempty = True: Exit For
    8. Next
    9. 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

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Print only "not empty" pages

    this should work with any number of pages, i tested it with 4 pages, 2x2

    vb Code:
    1. Set s = ThisWorkbook.Sheets("sheet2")
    2. With s
    3. .Activate
    4. s.Application.ActiveWindow.View = xlPageBreakPreview
    5.  
    6.  
    7. vs = 1
    8. hs = 1
    9. vp = .VPageBreaks.Count
    10. hp = .HPageBreaks.Count
    11. For i = 1 To vp + 1
    12.     For j = 1 To hp + 1
    13.         If i = vp + 1 Then
    14.             vb = .UsedRange.Columns.Count
    15.             Else
    16.             vb = .VPageBreaks(i).Location.Column - 1
    17.         End If
    18.         If j = hp + 1 Then
    19.             hb = .UsedRange.Rows.Count + .UsedRange.Row - 1
    20.             Else
    21.             hb = .HPageBreaks(j).Location.Row - 1
    22.         End If
    23.         Set r = .Range(.Cells(hs, vs), .Cells(hb, vb))
    24.         Debug.Print r.Address
    25.         For Each c In r
    26.             If Not IsEmpty(c) Then notempty = True: Exit For
    27.         Next
    28.         If notempty Then r.PrintOut
    29.         hs = hb + 1
    30.     Next j
    31.     hs = 1
    32.     vs = vb + 1
    33. Next i
    34. End With
    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

  4. #4

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Excel VBA - Print only "not empty" pages

    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

    Thanks in advance
    Attached Files Attached Files

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Print only "not empty" pages

    when i wrote the loop i forgot to rest the boolean, should be
    vb Code:
    1. If notempty Then r.PrintOut
    2. 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

  6. #6

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Excel VBA - Print only "not empty" pages

    - Hm, i put that noempty inside and still no luck

    - About the attach, here is Excel 2003 version - if that's the problem with opening it.
    Attached Files Attached Files

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Print only "not empty" pages

    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:
    1. Sub Printaj_Naslove()
    2. Application.ScreenUpdating = False
    3. Call UnLockS
    4. Dim s As Worksheet
    5. Dim r As Range, p As Range, vs As Integer, vb As Integer, vp As Integer
    6. hs As Integer, hb As Integer, hp As Integer
    7.  
    8.       Set s = ThisWorkbook.Sheets("Naslovi")
    9.       With s
    10.       .Activate
    11.       s.Application.ActiveWindow.View = xlPageBreakPreview
    12.       Set p = .Range(.PageSetup.PrintArea) ' set the print area as a range, so we can find the edges
    13.       vs = p.Range("a1").Row
    14.       hs = p.Range("A1").Column
    15.       vp = .VPageBreaks.Count
    16.       hp = .HPageBreaks.Count
    17.       For i = 1 To vp + 1
    18.           For j = 1 To hp + 1
    19.               If i = vp + 1 Then
    20.                   vb = p.Range("a1").Column + p.Columns.Count - 1
    21.                   Else
    22.                   vb = .VPageBreaks(i).Location.Column - 1
    23.               End If
    24.               If j = hp + 1 Then
    25.                   hb = p.Range("a1").Row + p.Rows.Count - 1
    26.                   Else
    27.                   hb = .HPageBreaks(j).Location.Row - 1
    28.               End If
    29.               Set r = .Range(.Cells(hs, vs), .Cells(hb, vb))
    30.               Debug.Print r.Address;
    31. '              For Each c In r
    32. '                  If Not IsEmpty(c) Then notempty = True: Exit For
    33. '              Next
    34. '              If notempty Then Debug.Print , r.Address; ' r.PrintOut
    35.                 If r.EntireRow.Hidden = False Then Debug.Print , r.Address;: r.PrintOut
    36.                  
    37.               notempty = False
    38.               Debug.Print
    39.               hs = hb + 1
    40.           Next j
    41.           hs = 1
    42.           vs = vb + 1
    43.       Next i
    44.       End With
    45.  
    46.  
    47.  
    48. Sheets("Sadrzaj").Select
    49.     Range("B1").Select
    50. Call LockS
    51. Application.ScreenUpdating = True
    52. 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
    Code:
    $B$3:$P$3     $B$3:$P$3
    $B$4:$P$4     $B$4:$P$4
    $B$5:$P$5     $B$5:$P$5
    $B$6:$P$6     $B$6:$P$6
    $B$7:$P$7     $B$7:$P$7
    $B$8:$P$8     $B$8:$P$8
    $B$9:$P$9
    $B$10:$P$10
    $B$11:$P$11
    $B$12:$P$12
    $B$13:$P$13
    $B$14:$P$14   $B$14:$P$14
    $B$15:$P$15   $B$15:$P$15
    $B$16:$P$16
    $B$17:$P$17
    $B$18:$P$18   $B$18:$P$18
    $B$19:$P$19   $B$19:$P$19
    $B$20:$P$20   $B$20:$P$20
    $B$21:$P$21   $B$21:$P$21
    $B$22:$P$23   $B$22:$P$23
    $B$24:$P$25   $B$24:$P$25
    $B$26:$P$27
    $B$28:$P$29
    $B$30:$P$31
    $B$32:$P$33
    $B$34:$P$35
    $B$36:$P$37
    $B$38:$P$39
    $B$40:$P$41   $B$40:$P$41
    $B$42:$P$43   $B$42:$P$43
    $B$44:$P$45   $B$44:$P$45
    $B$46:$P$47   $B$46:$P$47
    $B$48:$P$49   $B$48:$P$49
    $B$50:$P$51   $B$50:$P$51
    $B$52:$P$53   $B$52:$P$53
    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

  8. #8

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Excel VBA - Print only "not empty" pages

    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.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - Print only "not empty" pages

    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

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Print only "not empty" pages

    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

  11. #11

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved Re: Excel VBA - Print only "not empty" pages

    Thanks guys for your effort, help and detaild explanations.
    Method from Anhn worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width