How to prevent Userform Combobox show the drop down list if the item is only one? - excel

I have an array which contains customer's names as arrNama variable
I use a code to filter as I type which I've found in the internet (I am sorry I don't remember the link), something like this :
Sub cbNama_Populate(Optional fltr As String)
cbNama.List = Filter(arrNama, fltr, , vbTextCompare)
End Sub
Private Sub cbNama_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call cbNama_Populate(cbNama.Text)
If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown
End Sub
The If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown line is my modification, the original I found in the internet is just cbNama.DropDown.
I modified that line, because I thought with that IF conditon, it won't show the drop-down list if the list count of the combo box is only one. Yet it still show the drop-down list with that one item only, just like as if in the original code which has no IF condition.
Below is the example of what I mean :
At the time the combobox has only one item, the cbNama.ListIndex = 0 trigger the cbNama_Click sub. But the combobox still show the drop-down list, highlighted with blue on that one item.
My question:
How to prevent it showing the drop-down list when there is only one item?
Any kind of response would be greatly appreciated.
Thank you in advanced.
For the time being, I use cbNama.Enabled = False, this prevent the dropdown shows if there is one item only.

Related

How do you make the value in a control box update live in VBA?

I have a bare minimum of VBA experience, and I have to edit a tool someone else made a while ago. The values in the textbox for the form control will not update in real time. There are multiple textboxes (I think form control, not sure how to check.) where the user inputs values. However, the box does not update the value inside until a cell is selected.
I don't see anything in the code for this that would affect this. Here's an example from one of the Textboxes.
Private Sub SysA_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
With Sheets("SysA")
If KeyCode = 13 Then
If Len(.SysA.Value) = 5 Then
.SysANum.Activate
Else
MsgBox "Enter in a valid number!"
.SysANum.Value = ""
.SysANum.Activate
End If
End If
End With
End Sub
I've also looked in the properties for the textbox and don't see anything that might affect it. (AutoLoad is set to False and Enabled is set to True)

Problem with multiline string in userform

I am having a issue with multiline stings in a userform.
When a user selects a option, the code checks if the selected answer matches the correct answer and then shows if right or wrong. But in either case the code says it is wrong.
Example of a option is:
If you see the string, brush it off sideways
Place icepack/cold flannel to reduce swelling
Elevate area to reduce bloodflow
Private Sub OptionButton1_Click()
rowNum = Selection.Row - Selection.ListObject.Range.Row
DeclareVars
Column = examtable.ListColumns("Right ans").DataBodyRange(rowNum)
CorrectAns = examtable.ListColumns("Right ans").DataBodyRange(rowNum).Offset(0, Column)
RightWrong.Visible = True
If OptionButton1.Caption = CorrectAns Then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End If
End Sub
What i am expecting is that if correct, shows right, or wrong if incorrect
If I understand correctly you have multiple option buttons below one another? Option button one will always have the same caption. If it is selected it can become true (indicated by the black dot), but the caption will not change.
Lets say it looks like this
two option buttons
Then the upper is called OptionButton1, the lower OptionButton2.
You can check
If OptionButton1 then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End if
You could use a combobox (these are the drop down menus).
You could populate it with an array of the strings you want to test and ask if the answers concur.
When loading the user form use
Private Sub UserForm_Activate()
ComboBox1.list = Array("brush it off sideways", "Place icepack/cold flannel to reduce swelling", "Elevate area to reduce bloodflow")
End Sub
this will look as followed
The user form with the list open
Then you can say combobox.value = CorrectAns

Can two checkboxes be linked in a way that only one will ever be checked?

I am working on moving a form over to Excel. Due to the style of form I have to replicate the form with no changes.
On the form there are two check boxes, one for Full and one for partial.
I have the code spit out a warning box if both are selected. I think it would be easier to make it so that if one is checked the other will automatically uncheck.
' ERROR CHECK FULL/PARTIAL
If [F8] = True And [F9] = True Then
MsgBox ("FAI can not be both a Full and Partial")
ELSE
'actual code in middle.
END IF
There are two ways to do this. With Radio buttons, or checking if the other box is checked.
With radio buttons is simpler if one of them must be selected no matter what. You can enclose radio buttons on different frames to "group" them together as in the screenshot below. You can see that one on each frame is selected and you can only select one of the two on each frame.
If you really want to use a Checkbox, you can use the code below.
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
If Me.CheckBox2 = True Then Me.CheckBox2 = False
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2 = True Then
If Me.CheckBox1 = True Then Me.CheckBox1 = False
End If
End Sub
The first If checks if this is a click to Check the box, if it is, then it will check if the second box is already checked, if it is, it will un-check it.
This is assuming you are creating a UserForm, and these are not CheckBoxes on an Excel Sheet. The implementation would be the same, but the code will be slightly different.

VBA Excel : Populate TextBox with specific string of text when Option Button is selected

I developed a Form in Excel (2016) and I am trying (with VBA) to configure its behavior so that if the user selects a particular option button, two additional things happen:
A checkbox further down on the form is automatically checked.
A text box further down on the form automatically displays a set string of text.
More specifically, if the user selects OptionButtonABC, then ...
CheckBoxNone should become checked
TextBoxCompanyName (which does not display any text by default) should now display the string: 'ABC'.
I initially created a subroutine that just targeted condition 1, and everything worked fine. However, when I try to integrate the code required to handle condition 2, things start to unravel.
Below, you will see the code in its most current state. I have a Private Sub that initiates upon the Click event and then immediately defines a variable as a string. I then set up an if/then statement that specifies what should happen IF OptionButtonABC is true. Namely, CheckBoxNone should be selected (this works fine) AND TextBoxCompanyName should now display the string 'ABC'.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = ABC
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True And Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The desired behavior should (theoretically) be pretty easy to achieve, but my experience with VBA is still pretty limited, so I am reaching out to the community for a bit of advice. As I mentioned above, the code above works for the first condition. However, I am still unable to get the string of text ('ABC') to show up in the text box.
Thanks in advance for any advice you may offer.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = "ABC" 'String Values uses double quotes
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True
Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The operator AND must be used only in the IF statement comparison, not in what you want to do.

OnKey "Delete" clear combobox

I have a userform with multiple comboboxes. User can type in a new item or pick one from the list.
He can start typing first letters of wanted item but when he makes a mistake and starts with e.g. "b" instead of "n" he has to clear the combobox manually or find the item on the list using mouse or arrows.
I would like to quickly clear the box using the delete key so the user can start typing again. But I don't know where to put this line of code if correct (enter event, change event, some other maybe?).
Application.OnKey "{Delete}", "WyczyscPole"
First Excel needs to know which box the user is in, and then clear it.
I tried to create separate module with a variable that finds out the current combobox name and clear it in next step. But I don't know whether called sub below is even correct.
Sub WyczyscPole()
Dim NazwaPola As ComboBox
NazwaPola = frmZakupy.ActiveControl.Name
NazwaPola.Clear
End Sub
You can use the KeyDown event to capture the Delete key (KeyCode = 46) when the user is in the combobox.
Private Sub NazwaPola_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 46 Then
Me.NazwaPola.Value = vbNullString
End If
End Sub
Please try: frmZakupy.value=''
It's a good practice to don't use special characters like 'ś','ć' in any names.

Resources