hie
am looking for a vba code for controlling printing of excel sheet using activex check box in excel.
The code should only print the excel sheet when the checkbox is checked
and disable printing when unchecked.
i have tried the event below but it still prints and the msgbox is not showing
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End Sub
any suggestions?
You need to actually test if the checkbox is checked or not. Also, you need to place this code inside the ThisWorkbook module in the VBE.
Change sheet and checkbox name as needed:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").CheckBox1.Value = False Then
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End If
End Sub
Related
I'm trying to unload a userform ("DataEntry") when the active cell is not in the appropriate sheet (index number 2). I am getting a "Type Mismatch '13" after the messagebox, and I don't know why, can anyone help?
This sub is in a module, not the userform. I have tried setting up a sub in the userform and calling that (single line sub "Unload Me", but I get the same result.
Sub unloadform()
If ActiveSheet.Index <> 2 Then
MsgBox "The schedule must be selected to enter data"
Unload DataEntry
Exit Sub
End If
End Sub
Update:
I occurred to me to put this code in the userform initialize sub, (I don't want the userform to load if the correct sheet is not selected). Now I get a Runtime ErrorĀ '91'
Tried replacing Unload Me with Unload DataEntry (the name of the userform), (same result), and replacing it with Unload VBA.Userforms("DataEntry"), which throws a Runtime '13' error.
Private Sub UserForm_Initialize()
If ActiveWorkbook.ActiveSheet.Index <> 2 Then
MsgBox "Schedule must be selected to enter data"
Unload Me
Else:
'other code if correct sheet is selected
End If
End Sub
The VBA code below is meant to allow only certain users to tick & un-tick a checkbox. However, the problem is that if I check the box and then close the spreadsheet, when I re-open the excel file the 'tick' is no longer there. It's like the code does not save the 'tick' action. Basically, if I check the box I want that to stay like that, even after closing the spreadsheet. In the VBA code below i added ThisWorkbook.Save in order to save the "Tick" action but it simply saves the spreadsheet instead of saving the "Tick" in the check box. Could you please advise what's wrong in my code? I've asked this question before but unfortunately nobody seemed to be able to find a solution. thanks so much
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
If (UCase(Environ("username")) = "TESTNAME") Then
'Do nothing
Else
'Uncheck because user not matching
CheckBox1.Value = False
MsgBox ("You are not authorized to tick this box.")
End If
End If
ThisWorkbook.Save
End Sub
on my sheet1 I have a commandButton (bShow) and in the code behind:
Private Sub bShow_Click()
UserForm1.Show
End Sub
then I have a UserForm named UserForm1 with a checkbox (named checkBox1) and a button named saveButton, and with a code behind:
Private Sub saveButton_Click()
Sheets(1).Cells(1, 1).Value = UserForm1.CheckBox1.Value
End Sub
with this setup in cell("A1") appears TRUE or FALSE depending on the checkbox state
i hope it helps
EDIT1:
by opening the Form reading the value from the sheet:
Private Sub UserForm_Activate()
UserForm1.CheckBox1.Value = Sheets(1).Cells(1, 1).Value
End Sub
EDIT2:
be aware of error handling (eg.: what if cell value is neither TRUE nor FALSE) But that I leave to you
I have created a userform and added a chekcbox called 'DSSSContact'. I have written some code to check the contents of the userform before saving in the following sub:
Private Sub SaveandClose_Click()
When the form is initialized the checkbox is set greyed out but ticked - which I think means the value is null. The SavandClose sub code includes the following which I thought would make sure that checkbox is either set to true or false:
If DSSSContact = Null Then
MsgBox "DSSS Contact Test"
Exit Sub
End If
However when I save and close the form using the save and close command button without having made a selection on the checkbox the form saves the contents without triggering the message box. All the other checks that I have created to verify that textboxes and combo boxes are populated are working fine. I have also added the following code within the same sub:
Debug.Print Me.DSSSContact.Value
This prints null in the Immediate window
Any help much appreciated. Thanks
Try
If IsNull(DSSSContact.Value) Then
Check boxes in the forms are True = checked or False = Unchecked.
sub test()
MsgBox DSSSContact.Value
If DSSSContact.Value = "False" Then
MsgBox "DSSS Contact Test"
Exit Sub
End If
end sub
im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.
I am trying to prevent an excel workbook from closing based-on the state of a checkbox.
My sample code that is placed in 'ThisWorkbook' :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If CheckBox0.Value = "FALSE" Then
b = MsgBox("Are you sure that you want to submit?", vbYesNo)
End If
If b = vbNo Then Cancel = True
End Sub
At the moment, I am getting a run-time error 424 , object required and the debug points at the If CheckBox0.Value..... line.
I am not sure what I am doing wrong and I'm not a regular VBA user.
Help, please.
I think you're using an embedded Checkbox (ActiveX). If so, you need to specify the sheet that the checkbox lives in. You can't just say Checkbox0 because you can have Checkbox0 on multiple sheets.
The other thing is that you can't check for "False" because it's not a string, but a boolean value, so you can just use False because that is an inherent VBA keyword.
Here's what I think you're looking for:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim b As Long
If (Sheets("Sheet1").CheckBox1.Value = False) Then
b = MsgBox("Are you sure that you want to submit?", vbYesNo)
End If
If b = vbNo Then Cancel = True
End Sub
EDIT:
Forgot to mention to switch "Sheet1" for the sheet name that your checkbox lives in.