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
Related
Problem: I am building a Userform that has a 'Submit' and 'Cancel' button. I want the entire form to clear any entered data and close the form if the user hits the 'Cancel' button but I also am trying to build in the same functionality if the user hits the red 'X' in the top right corner. I'm unclear where I need to unload the form. I currently have it placed within the btnCancel_Click() method and I'm able to launch the form, enter some data and hit Cancel and it will close the form down.
But when I try to re-launch the form a 2nd time I get an error (I attached a picture of that message) that says
"Run-Time error '-2177418105 (80010007): Automation Error - The Callee (server [not server application]) is not available and disappeared; all connections are invalid. The call may have executed.
If I remove Unload Me from btnCancel_Click() then the form can close and re-open just fine, but any data I entered the first time will still be on the form and isn't cleared properly. I'm wondering if this is an Unload Me error or do I need to reset all form controls when I initialize the form?
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' how was the form closed?
' vbFormControlMenu = X in corner of title bar
If CloseMode = vbFormControlMenu Then
' cancel normal X button behavior
Cancel = True
' run code for click of Cancel button
btnCancel_Click
End If
End Sub
'******************************************************************
Private Sub btnCancel_Click()
mbCancel = True
Me.Hide
Unload Me
End Sub
'*********************************************************************
Private Sub UserForm_Initialize()
'Populate values for 2 combo boxes
lastEmp = Sheets("Form_Ref").Cells(Rows.Count, 1).End(xlUp).Row
Me.cmbBoxEmpName.List = Sheets("Form_Ref").Range("A2:A" & lastEmp).Value
lastBld = Sheets("Form_Ref").Cells(Rows.Count, 2).End(xlUp).Row
Me.cmbBoxBuildingName.List = Sheets("Form_Ref").Range("B2:B" & lastBld).Value
End Sub
'******************************************************************
Public form As New CheckOutForm
Sub testFormOptions()
'Button pressed within Excel will start program and show the userform
form.Show
End Sub
This is the easiest quick and dirty solution:
Delete Public form As New CheckOutForm from the code. Then add it in the testFormOptions():
Sub testFormOptions()
Dim form As New CheckOutForm
form.Show
End Sub
Some not-that-good VBA books/tutorials would even go a bit like this, but this is brutal:
Sub testFormOptions()
CheckOutForm.Show
End Sub
Anyway, now the problem with the predefined values in the form is solved.
For the clean and not-so-easy solution, consider writing a MVC framework around the form:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
this blogpost (disclaimer - mine!), which pretty much says what the above link proposes, but it does not have the errors from the question.
the old StackOverflow tutorial for UserForms
If you execute Unload, you destroy the form object. With other words, your (global) variable form gets invalid and if you issue a new form.show, you get the runtime error.
When, on the other hand, you just unhide the form, the form-object stays valid (it's just currently not visible) and all controls keep their value.
Either you do some housekeeping by resetting all controls when a form is displayed (use the UserForm_Activate-event), or you have to create a new form-object every time you want to display it (I would strongly advice not to use the name form as variable name to avoid confusion).
Sub testFormOptions()
dim myForm as CheckOutForm
myForm.Show
End Sub
I created a userform in Excel 2016 with two ListBoxes, using the Tools menu. I double clicked them to create subs and inserted code to check whenever one is selected.
Here is the code:
Private sub SaleType_Click ()
If SaleType.Value ="Core" then
'make sale label visible
QTDV.visible =true
' show core option btn
Core.Visible = true
End if
End sub
When I have a ListBox created from the toolbox this works, but every other time the form is run the saletype ListBox will be value null and this is a problem because I have a check to make sure the ListBox is not empty. Code follows:
If saletype = "" then
Dim msg as string Msg = " please select sale type"
Msgbox msg, and vbcritical
End if
If the ListBox presents value null it will not see it as empty and skip the check if I try saletype = null it still skips it.
I searched and it seems creating ListBoxes on the tool box is weird because Excel does not know what kind of control it is. I opted for creating the ListBoxes in VBA.
Private sub userform_initialize()
Dim saletype as msforms.Listbox
Set saletype = me.Controls.Add("Forms.ListBox.1", "SaleType")
But when running the form and selecting any option on the ListBox the SaleType_Click sub does not trigger.
If you want to implement event handling (like SaleType_Click) you need to declare the object with the WithEvents keyword:
Dim WithEvents saletype as msforms.Listbox
And if a variable/property is not set then its value doesn't exist, so instead of empty string ("") you need to validate for NULL - the IsNull function can do that (= NULL doesn't work as = only works with values):
If IsNull (saletype.Value) then
I just had a problem with my listbox_Click not being fired "every other time". Maybe it was when the same selection was desired twice in a row. Anyway, put this in the sheet code for the sheet that is "Show"ing the userform:
userformXXX.listboxYYY.ListIndex = -1
userformXXX.Show
This doesn't work if it is in the userform code.
I am writing an If statement to test if I i have an option selected in two combo boxes. The combo box being checked is generated from array with two values. One of those cells are blank so the top value is blank in the combo box.
My if statement is
The array is
Private Sub cbApplicationSelection_Change()
If (eMassWelcome.cbwesitechoose.text = "") Then
eMassWelcome.cbApplicationSelection.text = ""
MsgBox "A website has not been selected. Please select a website from the dropdown and try again."
End If
Exit Sub
End Sub
This works.
The problem is the pop up comes up 3 times and im not sure why. I've tried IsNull() and listindex = -1. IsNull() did not work and list index gave me the same result as above. All this is being done with vba in excel.
Have you placed any change event for cbApplicationSelection? If so, as you are changing it's value, the change event will also get triggered.
Private Sub cbApplicationSelection_Change()
If eMassWelcome.cbwesitechoose.text = "" Then
MsgBox "A website has not been selected. Please select a website from the dropdown and try again."
End If
Exit Sub
End Sub
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
I am trying to create a piece of vba code which will generate a message to the user when a user selects a value from a dropdown list/validation list in excel.
So far my script does this fine. However, I want the user to be able to select their first value from the dropdown list without getting a message, and then on the second, third or fourth time etc, if they should change their selection in the dropdown list I want the message to display.
Can someone please show me a way of doing this? Thanks in advance
'Check number of times a user has changed their selection
Dim rM As Range
Set rM = Range("M" & ActiveCell.Row).Cells.SpecialCells(xlCellTypeAllValidation)
If Intersect(Target, rM) Is Nothing Then
Else
MsgBox "changed"
End If
Add ..
Private BooRangeSelected as Boolean
... at the very top of the VBA code.
Set this value to False in the Initialize event.
Set this value to True when the dropdown list is updated.
If the value is already True when the dropdown list is selected then issue the message.
I'm assuming this macro is attached to a Workbook rather than a Form stored in "personal.xlsb". In Excel / view code double click the "ThisWorkbook" icon (see below
Private Sub Workbook_open()
Private BooRangeSelected as Boolean
BooRangeSelected = False
End Sub
This sets the variable to False once the Worksheet is opened. You can then set this to True once the dropdown has been selected
BooRangeSelected = True