Rooh
Rooh

Reputation: 21

VBA to stop printing worksheet in excel 2010

I am preparing journal voucher in excel 2010. I want to minimize the error. Hence i need a VBA code which helps to stop printing voucher if to cells are not equal. Please help.

Upvotes: 2

Views: 3966

Answers (1)

Gaijinhunter
Gaijinhunter

Reputation: 14685

What you want to do is create an event. In the VBA editor, double click ThisWorkbook tab in the Project Explorer and enter this code:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If Sheets("Sheet1").Range("A1") <> _
       Sheets("Sheet2").Range("A1") Then
    MsgBox "Cannot print. Values do not match"
    Cancel = True
    End If
End Sub

You can edit the cells (A1 on both sheet1 and sheet2 in my example) to be whatever you need. Make sure you save the workbook and open it with macros enabled for it to be active.

Upvotes: 2

Related Questions