-2

I am setting up a receipt printout from Excel 2010. I have multiple items on the worksheet and can print everything ok.

The receipt is to be printed in a busy environment and as such we just want the operators to enter the numbers and press CTRL+P.

Not all the items on the receipt will be used:

Item 1     10:00
Item 2      0.00   <--- This is an example of the row I do not want to print
Item 3     10.00
Total      20.00

The number of items could increase over time so the solution must be able to include the entire print range. Use of the hide function is not an option as it takes to long.

The solution must require no action by the user as they are not 'computer people'.

All cells are locked except those which require data to be entered to minimise input errors. i.e. VAT calculations

I had tried a VB routine but with no luck, hence the question.

EDIT: The VB I had written was-

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim RngCol As Range
    Dim i As Range
        Set RngCol = Range("B1", Range("B" & Rows.Count). _
        End(xlUp).Address)
            For Each i In RngCol
            If i.Value = 0 Then
        i.EntireRow.Hidden = True
        End If
    Next i End Sub

I have tried Jeeped's suggestion but some how the page size has now changed - won't change back either? Although Jeeped's suggestion has done what I wanted it is now ignoring the header which is needed although I can move the info to the main sheet.

jradich1234
  • 1,410
  • 5
  • 24
  • 29
  • You have chosen [vb.net] as well as [vba] but shown no code where we could try and determine the difference. While sharing similarities, they are NOT the same thing. Perhaps you could choose one or the other. No one like to see mud thrown at a wall to see what sticks. –  Aug 25 '15 at 09:46

1 Answers1

0

Use a conditional format rule. I always use rules based on formulas like =NOT($D1) to cover columns A:E depending on the value in column D but any of the others will do if you can determine criteria that equals zero. When you decide on how you want to handle the criteria, click Format and go to the Number tab. Choose Custom from the list down teh left and use ;;; (three semi-colons) for the Type:.

Click OK to accept the format and then OK to create the rule. Nothing in A:E will be visible if there is a blank or 0 in column D.

Alternate AutoFilter Method:

The individual worksheets do not have any standard events to do with printing but the workbook has a BeforePrint Event.

Go the workbook's VBE and locate ThisWorkbook in the Project Explorer then double-click it. Paste the following into the new pane on the right titled something like ThisWorkbook (Code).

Option Explicit

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Cancel = False
    With Worksheets("Sheet2")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range(.Cells(1, 2), .Cells(Rows.Count, 2).End(xlUp))
            .AutoFilter Field:=1, VisibleDropDown:=False, _
                        Criteria1:="<>0.00", Operator:=xlAnd, Criteria2:="<>"
        End With
    End With
End Sub

Edit to suit the actual column that holds the numbers. I've used column B. Note that the AutoFilter requires a header row.

Tap Alt+Q to return to your worksheet. Any method of printing the page will result in the AutoFilter Method being applied to column B and hiding any blanks or zeroes (in the form of 0.00) in that column.

Before:    Before Printing Receipt     After:    After Printing Filtered Receipt

As you can see from the sample images, printing to a .PDF filters the code column. I've also chosen not to display the little drop-down arrow normally associated with an AutoFilter.

You can unhide the rows with conventional unhide commands, clear the filter or just turn the autofilter off to get the rows back.

This latter method is a bit more involved but it gets rid of (aka hides or filters out) the undesired rows rather than simply not displaying the text in them. If need be, the worksheet could be unprotected for the duration of the print cycle.

Community
  • 1
  • 1
  • Thanks Jeeped. I tried your suggestion and got no joy. I've added the VB I had come up with to the original question, not sure why it won't show correctly though. I appreciated the hand holding as I'm not a genius at this! :D – Justin Webley Aug 25 '15 at 11:22
  • Ok, got it working now. Only issues left are: On print the header info is being over written, I can move this to the main sheet but the page length has now changed to custom. I am printing on an 80mm x 80mm but now it is set to custom, won't change back. – Justin Webley Aug 25 '15 at 11:40
  • I was just printing to PDF and it did not change. I'll have to look at it later. –  Aug 25 '15 at 11:45
  • Thanks I would appreciate it. Also how could I change this to work on column C as opposed to B? – Justin Webley Aug 25 '15 at 11:49
  • Change one line to `.Range(.Cells(1, 3), .Cells(Rows.Count, 3).End(xlUp))` or `.Range(.Cells(1, "C"), .Cells(Rows.Count, "C").End(xlUp))`. –  Aug 25 '15 at 11:58
  • Thanks, that works great. I've fixed the printing issue. Is it possible to un-hide the rows after the print has finished without the user having to press the FILTER button? I ask this as the people using the worksheet will be in a busy environment and I would like to avoid any possible mistakes or questions. Just noticed that this routine will not work on a protected sheet, is it possible to make it do so? i.e. the range this has to apply to is C5-C15 as C16 onwards are VAT calculations – Justin Webley Aug 25 '15 at 12:42
  • Perhaps some sort of OnTime scheduling with a 2-5 second delay. As I mentioned, I'll look into it a little more tomorrow. Oh! and that's good news about the printing size. –  Aug 25 '15 at 12:45
  • Thanks for all your help Jeeped. I've got it all working now. You deserve a pint, enjoy! – Justin Webley Aug 26 '15 at 14:57