excel中如何利用vba达到以下目的:如果某单元格的值显示为“FALSE”,则打印时提示有错误,并且不执行打印

2024-12-21 02:00:12
推荐回答(2个)
回答1:

ALT+F11进入VBA编辑.在左侧列表栏里的thisworkbook上双击,添加以下代码
Private Sub Workbook_BeforePrint(Cancel As Boolean)
For Each a In ActiveSheet.UsedRange
If a.Text = "FALSE" Then
Cancel = True
MsgBox "有错,退出打印!"
End If
Next
End Sub

回答2:

不要用自带的打印,增加打印控制事件
sub 打印()
''''如某单元格为A1
if [a1]=false then
msgbox "有错误!"
exit sub
end if
Application.Dialogs(xlDialogPrinterSetup).Show
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
end sub