I have a userform with a listbox displaying all items within a table.
the listbox has a double click event where;
if the row double clicked contains text it opens an edit form.
if the row double clicked is empty then a message box displays stating "the item is not valid for editing."
I'm looking to add a new feature to this where if a certain cell along the row contains "closed" a message box displays stating "this item is closed and not valid for editing.
I'm not very good at VBA and I'd appreciated any help that can be offered. below is the current code I have.
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
MsgBox "The selected item is closed and not a valid entry for editing"
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Thanks in advance :)
found a solution
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If (RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 10)) = "closed" Then
MsgBox "The selected item is closed and not a valid entry for editing"
Exit Sub
Else
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Replace
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
with
If Instr(RiskLogReviewListBox.Column(11, 0), "closed") > 0 Then _
But, I suppose that your code must exist in case of the two above situation. So, I would also suggest using of Exit Sub after the messages...
Related
I have the following two codes on a button: The (first code) aims to submit value of option button into worksheet cell:
For Each FormControl In Me.Controls
'Check only OptionButtons
If TypeName(FormControl) = "OptionButton" Then
'Check the status of the OptionButton
If FormControl.Value = True Then
'Set a variable equal to the Caption of the selected OptionButton
OptionButtonValue = FormControl.Caption
'We found the selected OptionButton so exit the loop.
Exit For
End If
End If
Next
'Store input in the worksheet
Sheets("Answer Sheet").Range("E80").Value = OptionButtonValue
To ensure an option button is selected before proceeding to next form, i have
the 'following code (second code):
Dim cnt As Integer
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
If ctl.Value = True Then cnt = cnt + 1
End If
Next ctl
If cnt = 0 Then MsgBox "Hello " & CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you
have not selected an answer! Please select an answer to proceed to next question. Thank you.",
vbInformation, "Please select an answer!" Else ScoreBoards.Show
Unload Me
MY CHALLENGES
Both codes above exists in my forms i.e. questions 1, 2, 3,...respectively, but can't seem to get the second code (that, which ensures an option button is selected before next form can be opened) to work by adding 'unload me' to the end of it, yet i want the form closed before proceeding to next. Adding 'unload me', pop-up the msgbox (which tells me to select an answer) but when i clicked okay on the msgbox, it closes the form (Question1) instead of returning me to same form to ensure an answer is clicked, then proceed to next form (Question2). However, when i remove the 'unload me', things work fine i.e. the msgbox popup when selection not made, returns to same form when okay on msgbox is clicked, and opens next form when selection made.
What i really want is: i want the second code above (which ensures an option button is selected before next form can be opened) to work as programmed and each form closed before proceeding to the next form.
Thank you in advance
PS:
The concept summary is:
On a userform (Question1), select an option button and submit the value to worksheet
Ensure an option button is selected:
if selected and button clicked, the next form(being Question2) should open
if not selected and button clicked, the msgbox (which tells me to select an answer), should popup
clicking okay on the msgbox, should return me to same form (Question1) so that i can select an option and proceed.
Try adapting your second code in the next way, please:
If cnt = 0 Then MsgBox "Hello " & _
CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you have not selected an answer! Please select an answer to proceed to next question. Thank you.", vbInformation, "Please select an answer!"
Exit For
Else
ScoreBoards.Show
Unload Me
End if
I have the following code that I thought would open a MsgBox warning if there is more than one selection made in ListBoxProjects.
However it opens the MsgBox if there is only one selection as well. It doesn't open the MsgBox if there are no selections. Bit stuck on this one, any help gratefully received.
If Me.ListBoxProjects.ListIndex > 1 Then
MsgBox "You can only edit Projects 1 at a time", vbExclamation, "Project Editing"
End If
ListIndex property returns the position of the selected item.
To achieve what you want you need you need to access the ItemSelected collection's Count property.
If ListBoxProjects.ItemsSelected.Count > 1 Then
MsgBox "You can only edit Projects 1 at a time", vbExclamation, "Project Editing"
End If
Use the Selected property array:
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
' do something
End If
Next i
I have a series of ComboBoxes that are populated based on the previous ComboBox selection. So for example, ComboBox3 is populated based on the value selected in ComboBox2. The "trigger" for populating ComboBox3 is the DropButtonClick action. I have a message box pop up when there is no value in ComboBox2. This is working successfully - code below.
If Me.ComboBox2.ListIndex = -1 Then
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
Exit Sub
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
My issues is once the Message Box appears (as a result of there being no value in ComboBox2) ComboBox3 still displays drops down values. Is there a way to undo the ComboBox3 DropButtonClick Event when there is no value in ComboBox2, so that ComboBox3 never drops down?
You can immediately close the drop-down window of the combo-box by simulating the "ESC" key:
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
' close immediately the combo's dropdown window
SendKeys "{ESC}{ESC}"
Is this something for you?
If Me.ComboBox2.Value = "" Then
ComboBox2.SetFocus
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
When you want to click on the dropbutton of combobox3 it automaticly goes back to combobox2.
I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If
I'm making an add records form for a spreadsheet of mine, and let's say that I want one of the controls to be a dropdown that is populated by unique entries under a certain column "type". However, I want to also make it such that the dropbox always has a initial option to "add new type" and upon such selection, it becomes a regular text box. How would I do this in VBA?
You cannot change a control type at run time. The easiest thing to do is create a combo box and a text box. Set the text box visibility to false. Then in the onchange event of the combo box your code will unhide the text box and hide the combo box. You will also need a save button so that when it is clicked it will add the option to the drop down, clear the text box, hide the text box, hide the button and unhide the drop down.
Okay, so here's my idea of how to tackle this.
Create 2 hidden elements (Visibility = False), one a TextBox and one a CommandButton.
Populate your ComboBox with the values from the sheet under column "type"
Add one more entry AddItem with wording such as "Add new item..."
When the user selects "Add new item...", change the Visibility of the TextBox & CommandButtons to True
When the user clicks the CommandButton, add the phrase to the column and add a new element to the ComboBox
I have created a mockup UserForm and code that does a little more than just this; it also styles the user entry to sentence case (consistency purposes) and checks to make sure the value isn't already in the column.
Excel Sheet with "type" column
UserForm with name labels
UserForm Code
Private Sub bAdd_Click()
Dim str As String
Dim rng As Range
Dim ro As Integer
'Makes sure there is an entry, adds it to the Sheet and then updates the dropdown
If Len(Me.tbNew) > 0 Then
'Converts user entry to "Sentance Case" for better readability
str = StrConv(Me.tbNew, vbProperCase)
'Finds out if the entry already exists
Set rng = Sheets(1).Range(Sheets(1).Cells(2, 1), Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1))
On Error Resume Next
Err.Number = 0
'Searches for duplicate; if found, then ListIndex of cbColor is modified without inserting new value (prevents duplicates)
ro = rng.Find(str, LookIn:=xlValues, LookAt:=xlWhole).Row
Debug.Print Err.Number
'Ensures a user doesn't add the same value twice
If Err.Number > 0 Then
Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row + 1, 1) = str
Me.cbColor.AddItem StrConv(Me.tbNew, vbProperCase), Me.cbColor.ListCount - 1
Me.cbColor.ListIndex = Me.cbColor.ListCount - 2
Else
Me.cbColor.ListIndex = ro - 2
End If
'Resets and hides user form entries
Me.tbNew = vbNullString
Me.tbNew.Visible = False
Me.bAdd.Visible = False
End If
End Sub
Private Sub bClose_Click()
Unload Me
End Sub
Private Sub cbColor_Change()
'Visibility is toggled based on if the user selected the last element in the dropdown
Me.bAdd.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
Me.tbNew.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
End Sub
Private Sub UserForm_Initialize()
'Populate from the sheet
For a = 2 To Sheets(1).Cells(Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1).Row
Me.cbColor.AddItem Sheets(1).Cells(a, 1)
Next
'Add option for new type
Me.cbColor.AddItem "Add new type..."
End Sub