简体   繁体   English

使用 VBA Excel 进行高级搜索

[英]Advanced search using VBA Excel

I have a list of data (20,000+ products) there is different pricing depending on a condition of the product, users need the ability to look up product pricing at certain grades quickly, my current challenge is using vlookup's fail as often the users will just keyword search and not have the exact description.我有一个数据列表(20,000 多种产品),根据产品的状况有不同的定价,用户需要能够快速查找某些等级的产品定价,我目前的挑战是使用 vlookup 的失败,因为用户经常会只是关键字搜索,并没有确切的描述。 I want to leave in the existing lookup using vlookup as that will remain a quick search when the user has the exact product title but want to add an advanced search in when they only have a keyword (I have tried data validation searchable lists but they are slow and unreliable)我想使用 vlookup 保留现有查找,因为当用户具有确切的产品标题但想要在他们只有关键字时添加高级搜索时,这将保持快速搜索(我已经尝试过数据验证可搜索列表,但它们是缓慢且不可靠)

I have created a Listbox but can't get the code to search my data set and then display the results,我创建了一个列表框,但无法获取代码来搜索我的数据集然后显示结果,

this is the code I'm using (Stock data has all my products and pricing) and my 2nd sheet is called Search where I want users to find what they are looking for这是我正在使用的代码(库存数据包含我所有的产品和定价),我的第二张表称为搜索,我希望用户在其中找到他们正在寻找的东西

enter code here

Private Sub cmdsearch_click()

Dim Rownum As Long
Dim Searchrow As Long

Rownum = 2
Searchrow = 2

Worksheets("Stock Data").Activate

Do Until Cells(Rownum, 1).Value = ""
If InStr(1, Cells(Rownum, 1).Value, txtkeywords.Value, vbTextCompare) > 0 Then
   Worksheets("Sheet1").Cells(Searchrow, 1).Value = Cells(rownnum, 1).Value
   Worksheets("Sheet1").Cells(Searchrow, 2).Value = Cells(rownnum, 2).Value
   Worksheets("Sheet1").Cells(Searchrow, 3).Value = Cells(rownnum, 3).Value
   Searchrow = Searchrow + 1
End If
Rownum = Rownum + 1



Loop

If Searchrow = 2 Then
MsgBox "Sorry No products found, please request a price"
End If

Lstsearchresults.RowSource = "SearchResults"

End Sub

As described in the comments, you could use Range.Find like this:如评论中所述,您可以像这样使用Range.Find

Private Sub cmdsearch_click()

Dim rowResult As Long
Dim strSearch As String

rowResult = 2
strSearch = txtkeywords.Value

Dim rowLast As Long
rowLast = Worksheets("Stock Data").Cells(Rows.Count, 1).End(xlUp).Row

Dim rngFound As Range
Dim firstAdress As String

With Worksheets("Stock Data").Range("A1:A" & rowLast)
    Set rngFound = .Find(strSearch, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
    If Not rngFound Is Nothing Then

        firstAddress = rngFound.Address
        Do
            Worksheets("Sheet1").Cells(rowResult , 1).Value2 = rngFound.Value2
            Worksheets("Sheet1").Cells(rowResult , 2).Value2 = rngFound.Offset(0, 1).Value2
            Worksheets("Sheet1").Cells(rowResult , 3).Value2 = rngFound.Offset(0, 2).Value2
            rowResult = rowResult + 1

            Set rngFound = .FindNext
        Loop While rngFound.Address <> firstAdress

    Else
        MsgBox "Sorry No products found, please request a price"
    End If
End With

Lstsearchresults.RowSource = "SearchResults"

End Sub

An even faster approach would be to store everything in an array and looping through that.一种更快的方法是将所有内容存储在一个数组中并循环遍历它。 When starting out however I found it easier to work with less abstract methods and learning step by step.然而,当我开始时,我发现使用较少抽象的方法和逐步学习更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM