Rick
Sun Mar 16 02:57:35 CDT 2008
What version of Excel are you using; actually, more importantly, what
version of VB is it (in the VB editor, Help/About Microsoft Visual Basic)?
I'm guessing your version of VB is less than 6.0 (I think things like
Filter, Split, Join, etc. were added then). If it turns out you have an
earlier version, I think I can dummy-up some coded functions to replace what
are built-in functions in my version. Let me know and I'll try and give you
an estimate of how long it might take me to do it. It's kind of late here
now and I'll being going to sleep shortly, so I'll read your reply and let
you know later today.
Rick
"affordsol" <affordsol@discussions.microsoft.com> wrote in message
news:3AD88696-B12D-49D0-B5F4-7D7877A660A0@microsoft.com...
> Hello Rick,
>
> Thanks a lot for your interest in my problem.
>
> I pasted your code accordingly but I get a "Complie error : undefined Sub
> or
> Function" message when the code reaches the line
> Persons = Filter(TextArray, "@" & TextBox1.Text, True, vbTextCompare)
>
> FILTER is the problem here !
>
>
> What do you suggest ???
>
>
> Thanks a lot and regards from Belgium,
> Herve+
> --
> Herve Hanuise
>
http://www.affordsol.be
>
>
> "Rick Rothstein (MVP - VB)" wrote:
>
>> > In an Excel workbook, sheet1 has a list of the customer names :
>> > col1=idx and col2=name
>> >
>> > I need to build up a user form with
>> > one Textbox
>> > one Listbox
>> > one Button (Exit)
>> >
>> > I need to enable the user to input the beginning of a name in the
>> > textbox,
>> > and, as each new typed character comes in, I want the listbox to
>> > display
>> > (in
>> > full) all of the names on sheet1 beginning with the actual content of
>> > the
>> > textbox to be listed in the listbox.
>> > The final selection is then done by a double click on the choosen name
>> > of
>> > the listbox.
>> >
>> > How can I code that in vba ??
>>
>> First, thank you for asking this question... I found this to be a fun
>> coding
>> project to develop.
>>
>> Okay, give the code after my signature a try (copy/paste all of the code
>> into the UserForm's code window). It assumes your TextBox is named
>> TextBox1
>> and your ListBox is named ListBox1. Also, it assumes that your Names list
>> starts in Row 2 of Column B. Both of these are set via Const statements
>> in
>> the UserForm's Initialize event; so you can change these to suit your
>> actual
>> conditions if necessary. I did not provide for the Exit button since I'm
>> assuming you already know how to do that and because I don't know what
>> code
>> you wish to execute when exiting the UserForm.
>>
>> There are a couple of "hidden" features you may find interesting (these
>> are
>> geared to letting you continue to work from the keyboard while typing).
>> Pressing the down arrow (or Right Arrow key when at the end of the text
>> you
>> are typing) when in the TextBox will move you into the ListBox where you
>> can
>> cursor up or down to the name you want and then press the Enter key to
>> select that name (and, of course, you can double-click on the name to
>> select
>> it if that is your preference instead). Also, if only one name remains in
>> the list, pressing Enter selects it into the TextBox directly (in the
>> same
>> way as if you had double-clicked on it). If, once in the ListBox, you
>> decide
>> you would like to return to the TextBox, just press the Left Arrow key
>> and
>> you will find yourself back in the TextBox at the end of the text that
>> you
>> have been typing.
>>
>> Okay, that is pretty much it. Let me know if this works for you or not
>> (and
>> if not, tell me what changes you need or want).
>>
>> Rick
>>
>> Dim TextArray() As String
>>
>> Private Sub UserForm_Initialize()
>> Dim X As Long
>> Dim LastRow As Long
>> Const StartRow As Long = 2
>> Const NamesColumn As String = "B"
>> TextBox1.Text = ""
>> TextBox1.EnterKeyBehavior = True
>> With Worksheets("Sheet1")
>> LastRow = .Cells(Rows.Count, NamesColumn).End(xlUp).Row
>> ReDim TextArray(0 To LastRow - StartRow + 1)
>> For X = StartRow To LastRow
>> TextArray(X - StartRow) = "@" & .Cells(X, NamesColumn)
>> ListBox1.AddItem .Cells(X, NamesColumn)
>> Next
>> End With
>> End Sub
>>
>> Private Sub ListBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
>> ByVal Shift As Integer)
>> With TextBox1
>> If KeyCode = vbKeyLeft Then
>> ListBox1.ListIndex = -1
>> .SelStart = Len(.Text)
>> .SetFocus
>> ElseIf KeyCode = vbKeyReturn Then
>> .Text = ListBox1.List(ListBox1.ListIndex)
>> .SelStart = Len(.Text)
>> .SetFocus
>> End If
>> End With
>> End Sub
>>
>> Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
>> TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
>> End Sub
>>
>> Private Sub TextBox1_Change()
>> Dim X As Long
>> Dim ListHeight As Long
>> Dim Individual As String
>> Dim Persons() As String
>> Persons = Filter(TextArray, "@" & TextBox1.Text, True, vbTextCompare)
>> If Len(TextBox1.Text) Then
>> If UBound(Persons) > -1 Then
>> With ListBox1
>> .Clear
>> For X = 0 To UBound(Persons)
>> .AddItem Mid$(Persons(X), 2)
>> Next
>> End With
>> Else
>> ListBox1.Clear
>> For X = 0 To UBound(TextArray)
>> ListBox1.AddItem Mid$(TextArray(X), 2)
>> Next
>> End If
>> Else
>> ListBox1.Clear
>> For X = 0 To UBound(TextArray)
>> ListBox1.AddItem Mid$(TextArray(X), 2)
>> Next
>> End If
>> End Sub
>>
>> Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
>> ByVal Shift As Integer)
>> With ListBox1
>> If KeyCode = vbKeyReturn Then
>> KeyCode = 0
>> If .ListCount = 1 Then
>> TextBox1.Text = .List(0)
>> TextBox1.SelStart = Len(TextBox1.Text)
>> Else
>> .SetFocus
>> .Selected(0) = True
>> .ListIndex = 0
>> End If
>> ElseIf KeyCode = vbKeyDown Or (KeyCode = vbKeyRight And _
>> .ListCount > 0 And TextBox1.SelStart = Len(TextBox1.Text)) Then
>> .SetFocus
>> .Selected(0) = True
>> .ListIndex = 0
>> End If
>> End With
>> End Sub
>>
>>