VBA code to prevent printing based on conditions

MissingInAction

Board Regular
Joined
Sep 20, 2019
Messages
85
Office Version
  1. 365
Platform
  1. Windows
Hi everyone.

I have a multipage (single sheet) Excel file that is used as an application form. Blocks that needs to be filled in are filled with yellow color. Once the person fills it in, the block's fill becomes blank. Furthermore, there is a dropdown list that has a few options. If the option "termination" is selected, all the other pages disappears. They are however still being printed.

My question is twofold: How can I prevent users from printing the form if there are yellow blocks on the sheet? Secondly, how can I customize the printing area based on a condition (B13="Termination") to prevent the other blank pages from printing? Page 1's area is A1:E44. My idea is to put this code in a command button.

I am currently using this code to prevent the normal print button from working:

VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
   If AllowPrint Then Exit Sub
   MsgBox "Please print via [Print] button"
   Cancel = True
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How can I prevent users from printing the form if there are yellow blocks on the sheet
One way ...

Here is a method to check a number of non-adjacent cells to see if any of those cells are blank
VBA Code:
    Dim tRng As Range, cel As Range
    Set tRng = Union(Range("B2:B16"), Range("D8:D12"), Range("F10:F11"), Range("A20"))
    For Each cel In tRng
        If Len(cel) = 0 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            Exit For
        End If
    Next cel

how can I customize the printing area based on a condition (B13="Termination") to prevent the other blank pages from printing? Page 1's area is A1:E44
VBA Code:
 If Range("B13") = "Termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else ActiveSheet.PageSetup.PrintArea = "A1:E999"
 
Upvote 0
Thanks for your reply. Customizing the printing area works great. Your code for checking required fields seems to check for any blank block. I would like to it to check only yellow blocks. Unless there is something wrong with where I pasted the code. This is what it looks like on the Sheet1 object in VBA:
VBA Code:
Sub Print_Form()
    If Range("B13") = "Termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else ActiveSheet.PageSetup.PrintArea = "A1:E147"
        Dim tRng As Range, cel As Range
    Set tRng = Union(Range("B2:B16"), Range("D8:D12"), Range("F10:F11"), Range("A20"))
    For Each cel In tRng
        If Len(cel) = 0 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            Exit For
        End If
    Next cel
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End Sub

EDIT. After clicking the OK button on the message stating that B2 requires input, the file does print. I suspect that is because of the two lines of code above End Sub.
 
Upvote 0
Rereading your code (not that I understand coding, but I can read it somewhat), I see that you meant for me to make changes to B2:B16, D8:D12, etc. So it means I need to modify those entries to reflect where my yellow blocks are.
Now the question is, not all my blocks are always yellow/required. For example, D13 becomes required based on the condition IF(B13="Termination") so that the user can enter the date when the person's account must be terminated.
 
Upvote 0
I see that you meant for me to make changes to B2:B16, D8:D12, etc
Yes - that is correct

Now the question is, not all my blocks are always yellow/required.
For example, D13 becomes required based on the condition IF(B13="Termination") so that the user can enter the date when the person's account must be terminated.

First of all determine which "yellow" is being used in your conditional formatting

Select a yellow cell and run macro below
For me, the messagebox returned 65535
It could be any number up to 8 digits long
VBA Code:
Sub GetColour()
    MsgBox ActiveCell.DisplayFormat.Interior.Color
End Sub

Then use something similar to what I suggested above ...
Rich (BB code):
    Dim tRng As Range, cel As Range
    Set tRng = Union(Range("B2:B16"), Range("D8:D12"), Range("F10:F11"), Range("A20"))
    For Each cel In tRng
        If cel.DisplayFormat.Interior.Color = 65535 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            Exit For
        End If
    Next cel
 
Upvote 0
Thank you. That works great. I also got the value of 65535 using the macro you provided.
Where can I add the code to print the sheet when no yellow blocks remain? This is the code I want to add:

VBA Code:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False

I have tried it in the following two ways. The first way caused it to not check the yellow blocks and just print it.

VBA Code:
Sub Print_Form()
    If Range("B13") = "Termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else ActiveSheet.PageSetup.PrintArea = "A1:E147"
    Dim tRng As Range, cel As Range
    Set tRng = Union(Range("B7:B9"), Range("D7:D8"))
    For Each cel In tRng
        If cel.DisplayFormat.Interior.Color = 65535 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            Else
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
            Exit For
        End If
    Next cel
End Sub

The second one caused the file to be printed 4 times before it stated that information is still required in B7.

VBA Code:
Sub Print_Form()
    If Range("B13") = "Termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else ActiveSheet.PageSetup.PrintArea = "A1:E147"
    Dim tRng As Range, cel As Range
    Set tRng = Union(Range("B7:B9"), Range("D7:D8"))
    For Each cel In tRng
        If cel.DisplayFormat.Interior.Color = 65535 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            Exit For
            Else
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
                IgnorePrintAreas:=False
            End If
    Next cel
End Sub
 
Upvote 0
How about

VBA Code:
Sub Print_Form()
    If Range("B13") = "Termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else ActiveSheet.PageSetup.PrintArea = "A1:E147"
    Dim tRng As Range, cel As Range, yellow As Boolean
    Set tRng = Union(Range("B7:B9"), Range("D7:D8"))
    For Each cel In tRng
        If cel.DisplayFormat.Interior.Color = 65535 Then
            MsgBox "Cannot print" & vbCr & cel.Address(0, 0) & " requires a value"
            yellow = True
            Exit For
        End If
    Next cel
    If Not yellow Then ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub
 
Upvote 0
Thank you Yongle. I feel like I'm getting closer to the final result.
I have made a few additions to the code to do the same checking for blank values, but this time it is for checkboxes. Its probably not the most efficient way, but it works. Here is the code:

VBA Code:
Sub Print_Form()
    If Range("B13") = "Termination" Or Range("B13") = "termination" Then ActiveSheet.PageSetup.PrintArea = "A1:E44" Else If Range("B69") = "No" Then ActiveSheet.PageSetup.PrintArea = "A1:E82" Else ActiveSheet.PageSetup.PrintArea = "A1:E147"
    Dim tRng As Range, cel As Range, alarm_cell As Range, equipment_cell As Range, yellow As Boolean
    Set alarm_cell = Range("G55")
    Set equipment_cell = Range("H55")
    Set eunomia_cell = Range("I55")
    Set tRng = Union(Range("B7:B9"), Range("D7:D8"), Range("B11"), Range("D11"), Range("B13"), Range("D13"), Range("C14"), Range("B15"), Range("B23:B24"), Range("E48"), Range("D52"), Range("B49:B52"), Range("C53"), Range("B54"), Range("B62:B73"), Range("D69"), Range("D62"), Range("B87:B89"))
    For Each cel In tRng
        If cel.DisplayFormat.Interior.Color = 65535 Then
            MsgBox "Cannot print" & vbCr & "Block " & cel.Address(0, 0) & " requires a value"
            yellow = True
            Exit For
        End If
    Next cel
    If alarm_cell.DisplayFormat.Interior.Color = 65535 Then
        MsgBox "Please select a town for which keys and an alarm code is required"
        yellow = True
    End If
    If equipment_cell.DisplayFormat.Interior.Color = 65535 Then
        MsgBox "Please select the required equipment"
        yellow = True
    End If
    If eunomia_cell.DisplayFormat.Interior.Color = 65535 Then
        MsgBox "Please select the Eunomia risk department responsibility"
        yellow = True
    End If
    If Not yellow Then ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub

I have one more problem. The code I used to disable the standard print button, now also prevents my macro from printing, even if all the conditions are met. This is the code:

VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
   If AllowPrint Then Exit Sub
   MsgBox "Please print via [Print] button"
   Cancel = True
End Sub

Is there a way to bypass that particular piece of code when the Print_Form macro is run?
 
Upvote 0
deleted by Yongle
 
Last edited:
Upvote 0
Insert
VBA Code:
AllowPrint = Not yellow
Before
VBA Code:
If Not yellow Then ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top