Hiding command button before printing

AndrewGKenneth

Board Regular
Joined
Aug 6, 2018
Messages
59
Hi there,

I am having some trouble getting my code to work. I would like commandbutton1 in sheet1 to hide so it is not visible when the form is printed by the user. Could someone please help me to identify where I am going wrong with my code? I have posted it below.

Private Sub Workbook_BeforePrint (Cancel As Boolean)
Sheets("Sheet1").Commandbutton1.Visible = False
End Sub

Thanks in advance
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I'm not sure for an ActiveX box, but if it's from the Forms menu, you can set the .PrintObject property.

Code:
Private Sub Workbook_BeforePrint (Cancel As Boolean)
    Sheets("Sheet1").Shapes("Button 1").PrintObject = False
End Sub

It looks like an ActiveX might have the same property, that could be set at design time.
 
Upvote 0
Your code looks ok to me (I tested lower case button - it works)
- assumes that the command button is on Sheet1, and not on a userform

for active-x button either
Code:
Sheets("Sheet1").CommandButton1.Visible = False
Sheets("Sheet1").Shapes("CommandButton1").Visible = False
form control button
Code:
Sheets("Sheet1").Shapes("Button 1").Visible = False

Is the command button name correct?
- check with this
Code:
Sub TestShapes()
    Dim objX As Object, msg As String
    With Sheets("Sheet1")
        For Each objX In .OLEObjects
            If TypeName(objX.Object) = "CommandButton" Then
                msg = msg & vbCr & objX.Name
            End If
        Next
    End With
    MsgBox msg
End Sub

Where is Private Sub Workbook_BeforePrint located?
- it should be in ThisWorkbook module
- add a message box to prove that it triggers when printing
 
Last edited:
Upvote 0
The problem that I have with the OP code is how to make the button re-appear once the printing is complete.
Yes, a simple macro would do that, but how to avoid running that second macro.
 
Last edited:
Upvote 0
If I'm not mistaken, both Form Controls and Active X Controls have a "Print Object" option that can be toggled in the "Format Control" menu. This requires no code to set the value each time... the button would be visible all the time on the worksheet, but as soon as you print, the button does not show up on the hard copy.

I guess this is only relevant if you are not using vba to add a button to a sheet dynamically.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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