Userform listbox visibility

wwbwb

Well-known Member
Joined
Oct 20, 2003
Messages
513
Greetings all,

I have a userform that I use to input names on a sheet. When the userform opens, textbox1 will populate depending on if a certain cell contains a name based on this line.

UserForm2.TextBox1.Value = Worksheets("list").Range("e" & res).Value

This works fine. I recently added the following code to the userform to have a listbox appear to give the user suggestions as they type a name into textbox1.

Code:
Private Sub textbox1_change()
'Code adapted from youtube.com/watch?v=e-3DrNiQ31I
Dim i As Integer

ListBox1.Clear

For i = 2 To 900
If UCase(Left(Worksheets("list").Cells(i, 5), Len(TextBox1.Text))) = UCase(TextBox1.Text) Then
 ListBox1.AddItem Worksheets("List").Cells(i, 5)
End If
Next i
ListBox1.Visible = True

End Sub

Private Sub TextBox1_Exit(ByVal cancel As MSForms.ReturnBoolean)

On Error Resume Next
ListBox1.Visible = False
End Sub

Private Sub listbox1_dblclick(ByVal cancel As MSForms.ReturnBoolean)

TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
ListBox1.Visible = False
End Sub

Private Sub UserForm_Initialize()
ListBox1.Visible = False
End Sub

Everything works fine, with one little hang up. When the userform opens, and textbox1 populate with nothing, great. It works as expected. When the userform opens, and textbox1 populates with something, the listbox is visible. It goes away if I go to another box like it should. I understand it does because textbox1 changed, but I'd like to find a way for it to only appear when a user types something. Any thoughts?
 
Oops, got these lines the wrong way round
Code:
  UserForm2.Show
  [COLOR=#ff0000]MyDisableEvents = False[/COLOR]
Swop them round
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top