You are here:

Excel/Follow-up regarding not printing blank rows

Advertisement


Question
Tom -
For an earlier inquiry, you provided a VBA script that allowed blank rows in the middle of a data range to be omitted from the print range.  In the answer, you made some assumption, one of which was that Column A was the determining column of whether a row was blank.  I have a similar need, but I want Column B to be the determinate column. Here is the code you'd provided before:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
On Error GoTo ErrHandler
' change "Sheet1" to match your sheet name exactly,
' including case.
If ActiveSheet.Name = "FAD 9802" Then
Application.EnableEvents = False
Set Rng = Range(Cells(640, 1) _
  .End(xlUp).Offset(2, 0), Cells(640, 1))
Rng.EntireRow.Hidden = True

ActiveSheet.PrintOut
Rng.EntireRow.Hidden = False
Cancel = True
End If
ErrHandler:
Application.EnableEvents = True
End Sub

Thanks,
Ted

Answer
Ted,

the second argument of Cells is the column.  It can be specified as a number or as the column Name (such as "B"  or "IV" as examples).  In the below code, I have changed the 1 (column A) to a "B" to make it easier to see.


Private Sub Workbook_BeforePrint(Cancel As Boolean)
On Error GoTo ErrHandler
' change "Sheet1" to match your sheet name exactly,
' including case.
If ActiveSheet.Name = "FAD 9802" Then
Application.EnableEvents = False
Set Rng = Range(Cells(640, "B") _
 .End(xlUp).Offset(2, 0), Cells(640, "B"))
Rng.EntireRow.Hidden = True

ActiveSheet.PrintOut
Rng.EntireRow.Hidden = False
Cancel = True
End If
ErrHandler:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.