Performance issue in Dynamic Dropdown Search list

2019-08-23 09:14发布

I have created dropdown list which gives suggestions from existing data (like google search). I have followed the steps mentioned in the link to get the expected result. I have changed the process to VBA Code since the column range that has data can increase in time (Formula will be inserted for a range dynamically when a new entry is added). Now the problem is since the column have more than 20 000 rows it takes more time to display the suggestions when a key is pressed.

I need to get data from users in a form and store it in an excel sheet. Form will mostly contain dropdown search lists with search list having almost 100k values. Search list is maintained in a different excel sheet and the size of the list will increase every week. Is it ok to use Excel VBA to achieve this? If so how to improve excel performance?

I am using Excel 2010 in Windows Server 2008 OS. I can load the data to SQL Server Table. If this is not possible in Excel is there any way that I can use SQL Server to achieve this?

3条回答
甜甜的少女心
2楼-- · 2019-08-23 09:44

I don't know about performance, but give the following a try. Your data is supposed to be in sheet "db", from row 2 onwards. Insted of a combobox, I place a textbox (TextBox1) and a list (ListBox1) in a UserForm.

Private Sub TextBox1_Change()
    Dim v As String
    Dim YourInput As String
    Dim iIdx As Long
    Dim CharNumber As Integer

    YourInput = TextBox1.Text

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    ListBox1.Clear

    lLastRow = Worksheets("db").Cells(3, 1).End(xlDown).Row
    CharNumber = Len(YourInput)

    For iIdx = 2 To lLastRow
        v = Worksheets("db").Cells(iIdx, 1).Text
        If LCase(Left(v, CharNumber)) = LCase(YourInput) Then
            ListBox1.AddItem v
        End If
    Next

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub

Private Sub ListBox1_Click()
    MsgBox ("Your selection: " & ListBox1.Text)
    'do your stuff
End Sub
查看更多
Animai°情兽
3楼-- · 2019-08-23 09:47

I can't solve what you pretend. I was able to fill a list according to input rather fast. However, code finds your input anywhere in the string, not in the beginning. Depending on your data, the following code might help you, or may be you can reformulate it to fit your needs. Again, data is supposed to be in sheet "db", from row 2 onwards, and there is a textbox (TextBox1) and a list (ListBox1) in a UserForm.

Private Sub UserForm_initialize()
    Dim lLastRow As Long

    'Get last row of data
    lLastRow = Worksheets("db").Cells(3, 1).End(xlDown).Row
    ReDim DirArray(1 To 1)
    j = 1

    'Add all the data to an Array (DirArray)
    For i = 3 To lLastRow
        ReDim Preserve DirArray(1 To j)
        DirArray(UBound(DirArray)) = Worksheets("db").Cells(i, 1).Value
        j = j + 1
    Next
End Sub

Private Sub TextBox1_Change()
    Dim YourInput As String

    YourInput = TextBox1.Text

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    ListBox1.Clear
    LArray = Array()

    LArray = Filter(DirArray, YourInput, True, vbTextCompare)

    ListBox1.List = LArray

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    End Sub

Private Sub ListBox1_Click()
    MsgBox ("Your selection: " & ListBox1.Text)
    'do your stuff
End Sub
查看更多
Luminary・发光体
4楼-- · 2019-08-23 09:56

I did something similar to a google search in my excel app. My code waits 2 seconds after the last keystroke, then runs through another sheet to get close matches. This way it wasn't running through the DB whilst typing..

Wait until user has stopped typing in ComboBox to run macro (VBA)

Someone had suggested I actually look at this post but it was not helpful to my particular challenge.

查看更多
登录 后发表回答