2017-09-21 19 views
0

我创建了一个下拉列表,它提供了来自现有数据(如谷歌搜索)的建议。我已按照link中提到的步骤获得预期结果。我已经将流程更改为VBA代码,因为具有数据的列范围可能会随时间增加(添加新条目时,公式将动态插入一个范围)。现在的问题是,由于该列有超过20000行,因此按下按键时需要更多时间来显示建议。Excel中的动态下拉搜索列表中的性能问题

我需要从用户的表单中获取数据并将其存储在Excel表格中。表单将主要包含具有接近100k值的搜索列表的下拉搜索列表。搜索列表保存在不同的Excel表格中,并且列表的大小将每周增加。使用Excel VBA实现这一点可以吗?如果是的话如何提高excel性能?

我在Windows Server 2008操作系统中使用Excel 2010。我可以将数据加载到SQL Server表。如果这在Excel中是不可能的,我有什么方法可以使用SQL Server来实现这一点?

回答

0

我不知道性能,但给以下尝试。您的数据应该放在第2行以后的表“db”中。由于没有组合框,我在用户窗体中放置了一个文本框(TextBox1)和一个列表(ListBox1)。

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 
+0

感谢您的答案:)但我仍然看到性能问题。按下键时,列表框需要至少5到10秒才能更新。 – Vijay

+0

我明白了....看看新的答案。 – CMArg

0

我无法解决你假装的东西。我能够根据输入很快地填写一个列表。但是,代码会在字符串中的任何位置找到您的输入,而不是在开始处。根据您的数据,以下代码可能对您有所帮助,或者您可能会重新配置它以适应您的需求。同样,数据应该位于第2行以后的表“db”中,并且在用户窗体中有一个文本框(TextBox1)和一个列表(ListBox1)。

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 
0

我在我的Excel应用程序中做了类似于谷歌搜索的操作。我的代码在最后一次击键之后等待2秒钟,然后通过另一张表进行近距离匹配。这样,它不是通过数据库,同时输入运行..

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

有人曾建议我其实看这个帖子,但它不利于我的特殊挑战。