I have an Excel UserForm, which submits data to a worksheet. I have built in data validation that requires each text box to have a value entered, e.g.:
Private Sub Button_Submit_Click()
'Data Validation
If Me.txtCVS.Value = "" Then
Me.txtCVS.SetFocus
MsgBox "'CVs Screened' is a mandatory field. Enter daily figure or zero.", vbOKOnly, "Required Field"
Exit Function
However, I don't know what code to use to require a selection from a list box - users select their name from the list box before entering in their daily figures. Some users, it seems, can't remember to click their name(!!!).
Does anybody have a piece of workable code I can use to require a selection before submitting results? It doesn't seem to work in the same way as the text box text above.
You can use the below function which iterates through all the items in the listbox and returns true/false if anything is selected or not.
Private Sub Button_Submit_Click()
If Not IsAnythingSelected(ListBox1) Then
MsgBox "Please select your name"
End If
End Sub
Function IsAnythingSelected(lBox As Control) As Boolean
Dim i As Long
Dim selected As Boolean
selected = False
For i = 1 To lBox.ListCount
If lBox.selected(i) Then
selected = True
Exit For
End If
Next i
IsAnythingSelected = selected
End Function
Try changing
For i = 1 to lBox.ListCount
to the following:
For i = 0 To lBox.ListCount - 1
Related
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
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
Alright, so here’s the code for my UserForm:
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call InventoryEntryBox_Initialize
End Sub
Private Sub SubmitButton_Click()
Dim emptyRow As Long
'Make Inventory Test sheet active
Worksheets("InventoryTest").Activate
'Transfer Information
Worksheets("InventoryTest").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = PartNumberComboBox.List
Worksheets("InventoryTest").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = LocationTextBox.Value
Worksheets("InventoryTest").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0).Value = QuantityTextBox.Value
Worksheets("InventoryTest").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = CommentsTextBox.Value
Call InventoryEntryBox_Initialize
End Sub
Private Sub InventoryEntryBox_Initialize()
'Fill PartNumberComboBox
PartNumberComboBox.List = ActiveWorkbook.Sheets("Test2").Range("B2:B43").Value
'Empty Location Text Box
LocationTextBox.Value = ""
'Empty Quantity Text Box
QuantityTextBox.Value = ""
'Empty Comments Text Box
CommentsTextBox.Value = ""
'Unit Of Measure auto-fill box
'Description auto-fill box
DescriptionFormula.Value = Application.WorksheetFunction.VLookup(PartNumberComboBox.List, ActiveWorkbook.Sheets("Test2").Range("B2:D43"), 3, False)
'Set focus on Empty Part Number text box
PartNumberTextBox.SetFocus
End Sub
Now, the idea with this code is to have User Form so someone can fill it out (the PartNumberComboBox, LocationTextBox, QuantityTextBox, and CommentsTextBox) in order to catalogue inventory on what items are in what places and in what quantity. When someone fills out an item number in the PartNumberComboBox, the DescriptionFormula will auto-fill with data from a list (Noted as PartDescription, which includes D2:D43 in the sheet Test2).
My problem, however, is two-fold: The DescriptionFormula.Value box does not auto-fill with data when someone enters in a part number in the PartNumberComboBox, and when I try to hit the “submit” button to confirm the data entered in the form and put it in the areas specified in the code, I get a dialogue box that pops up and says “Run-time error 70: Permission Denied”, then the "PartNumberComboBox.List = ActiveWorkbook.Sheets("Test2").Range("B2:B43").Value" is highlighted when I go to debug.
I’m not sure if it’s a problem with my code, or if it’s a limitation of Excel or Vlookup based on what I’m doing… or if it’s something else. Any kind of help anyone could offer would be a blessing at this point.
I've actually solved my own question:
So it turns out that I needed to write a new sub for this particular equation, not embed it in my Initialize sub. Here's what I wrote to get it to work:
Private Sub PartNumberComboBox_Change()
Me.UnitOfMeasureFormula.Value = Application.WorksheetFunction.VLookup(Me.PartNumberComboBox.Value, Sheets("sheet2").Range("a2:c43"), 3, False)
Me.DescriptionFormula.Value = Application.WorksheetFunction.VLookup(Me.PartNumberComboBox.Value, Sheets("sheet2").Range("a2:c43"), 2, False)
End Sub
while changing my "Description auto-fill box" to this:
'Empty Description label
DescriptionFormula.Value = ""
What this does is empty out the box that the auto-fill will go in on startup of the Userform, then using a _Change sub with the ComboBox that the formula is referencing so it alters the DescriptionForumula based on what the ComboBox says with a simple VLookup formula. Now it's working smoothly and I'm back on track!
I have search for the same question, and I saw a few of the similar post, however my Userform still can't work. I am new to VBA and Userform.
I have a total of 12 Checkboxes (12 Months), and I have to check that at least one of the CheckBox is checked.
Dim atLeastOneChecked As Boolean
atLeastOneChecked = False
Dim ctrlNCK As Control
For Each ctrlNCK In Controls
If TypeName(ctrlNCK) = "chkMonth" Then
If ctrlNCK.Value = True Then atLeastOneChecked = True
End If
Next ctrlNCK
If Not atLeastOneChecked = True Then
MsgBox "Month cannot be empty.", vbExclamation, "Input Data"
Exit Sub
End If
This is untested, but try:
If Left(ctrlNCK.Name,8) = "chkMonth" Then
I wouldn't ever expect the Type to be "chkMonth". This assumes that each relevant CheckBox name starts with "chkMonth".
If you only ever want one, and only one, month chosen, I'd consider using OptionButtons in a Frame.
If (chbindian + chbchinies + chbitalian + chbsindian) = 0 Then
MsgBox "Please select at least Two checkbox"
Exit Sub
End If
try this
(chbindian + chbchinies + chbitalian + chbsindian) replace this names with your checkbox name
Using similar approach to the question code I came up with this:
Private Sub CommandButton1_Click()
Dim chk As Control
Dim i As Long
For Each chk In Me.Controls
If TypeOf chk Is MSForms.CheckBox Then
If chk = 0 Then
i = i + 1
End If
End If
Next
If i = 12 Then
MsgBox "Please select at least one month!"
Exit Sub
End If
MsgBox "You selected at least one month!"
End Sub
To explain:
The For loop is checking one by one each CheckBox on the UserForm.
If the value of the CheckBox is 0 (False) then the counter which is the i variable equals it's own value at the end of the previous iteration (current value) + 1.
Each CheckBox with a value of 0 (False) meaning it's 'unchecked' adds 1 to the counter and thus, once the loop is complete i will equal anywhere between 0 (all CheckBoxes are checked) and 12 (no CheckBoxes are checked).
After the loop is exited the next if statement checks if the value of the counter i is equal to 12 then a MsgBox displays "Please select at least one month!" otherwise whatever is meant to happen next will now execute (In my example its another MsgBox advising "You selected at least one month!").
NOTE: If more CheckBoxes were added to the UserForm at a later time for whatever reason, my solution would be flawed as it will check ALL CheckBox commands on the form.
To work around that you can put the required CheckBoxes on a Frame on the UserForm.
Assuming the Frame is named Frame1 all you would need to do is adjust the For line to specify for each CheckBox in Frame1 on the UserForm like so: For Each chk In Me.Frame1.Controls
I am working on pulling data out of a standardized form in excel. There is a Forms Control CheckBox that I need the state of. Apparently the only way to get this is from the cell link, where the value is placed into a cell. The problem is, whomever put this form together did not set a cell link. Is there any way to do this using VBA at run time. There are many of these forms that I must go through, so I'm trying to avoid doing it manually.
I think you are referring to a forms checkbox control placed on a worksheet, in which case you can get the state of the control without setting a cell link. Like this:
Sub HTH()
Dim iLoop As Integer
'// Get value of check box by its index
MsgBox (GetCheckBoxState(1))
'// Get value of check box by its name
MsgBox (GetCheckBoxState("Check Box 1"))
'// Loop through all checkboxes and get values
For iLoop = 1 To ActiveSheet.CheckBoxes.Count
MsgBox (GetCheckBoxState(iLoop))
Next
End Sub
Function GetCheckBoxState(vCheckBox As Variant) As String
Select Case ActiveSheet.CheckBoxes(vCheckBox).Value
Case xlOn
GetCheckBoxState = "Checked"
Case xlOff
GetCheckBoxState = "UnChecked"
Case xlMixed
GetCheckBoxState = "Mixed"
End Select
End Function
If you are referring to a check box control on a userform then as Tim pointed out it should be a case of something like:
MsgBox (UserForm1.CheckBox1.Value)