开发者

VBA to stop printing worksheet in excel 2010

开发者 https://www.devze.com 2023-03-26 09:53 出处:网络
I am preparing journal voucher in excel 2010. I want to minimize the error. He开发者_运维知识库nce i need a VBA code which helps to stop printing voucher if to cells are not equal. Please help.What yo

I am preparing journal voucher in excel 2010. I want to minimize the error. He开发者_运维知识库nce i need a VBA code which helps to stop printing voucher if to cells are not equal. Please help.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消