简体   繁体   English

搜索范围内的文本并显示包含该文本的所有行 - VBA

[英]Search for a text in a range and display all rows which contain the text - VBA

I would be very grateful if someone can help me with the following:如果有人可以帮助我解决以下问题,我将不胜感激:

I want to search for a text (using Textbox) in a very large database.我想在一个非常大的数据库中搜索文本(使用文本框)。 (for example searching for: Iron). (例如搜索:Iron)。 The results I'm expecting would be like the following: "Red Iron", "Iron grey", "A very long iron" + copying the entire rows to another sheet (with the textbox name) and find the lowest price which is in range (D2:J).我期望的结果如下:“红铁”、“铁灰”、“一个很长的铁”+ 将整行复制到另一张纸(带有文本框名称)并找到其中的最低价格范围 (D2:J)。 D1, E1, F1, G1, H1, I1, J1 are the suppliers. D1、E1、F1、G1、H1、I1、J1是供应商。 If it's possible I want to show the supplier name and the lowest price in a msgbox.如果可能,我想在 msgbox 中显示供应商名称和最低价格。

I want to search in range A:A.我想在 A:A 范围内搜索。

Can anybody help me with this?有人可以帮我解决这个问题吗?

Many thanks, N.非常感谢,N。

Couple things to help you start, in case you've not tried any coding yourself...一些事情可以帮助您开始,以防您自己没有尝试过任何编码......

.1) You can give yourself a Userform to input the desired term (you should be able to make the Userform yourself). .1)您可以给自己一个用户表单来输入所需的术语(您应该能够自己制作用户表单)。 Make sure to save that term outside of the code, so you can carry it through (in case you write multiple macros for each part):确保将该术语保存在代码之外,以便您可以执行它(以防您为每个部分编写多个宏):

Public burp as Text
Sub 
    Set burp = Userform(1).Textbox(1).Value 'Will need to tweak
End Sub

Sub NameOfNextSub()

.2) I haven't played much with the Find function, but I have done something similar to what you're wanting where I loop and match. .2) 我没怎么玩过 Find 功能,但我做了一些类似于你想要的循环和匹配的东西。 If there's a match, it pastes the row matched within to the end of another sheet如果匹配,则将匹配的行粘贴到另一张纸的末尾

Dim LR as Long
LR = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

For i = 1 to LR
    If IsError(WorkSheetFunction.Match(*burp*,cells(i,2)),0)>0 Then
        Sheets("Sheet1").Row(i).Copy
        Sheets("Sheet2").Row(i).PasteSpecial xlPasteValues
        Else:
        End If
Next i
Delete_Empty_Rows 'runs macro named "Delete_Empty_Rows"

Google delete empty rows... you should gets tons of hits, doing it different ways;谷歌删除空行......你应该得到大量的点击,用不同的方式; pick what feels best for you.选择最适合你的。 Make sure it runs on Sheet2.确保它在 Sheet2 上运行。

That's a pretty lazy way of doing it, but it will work.这是一种非常懒惰的方法,但它会起作用。

.3) Filter Sheet2 based on whatever column has cost, xlAscending. .3) 根据任何列的成本 xlAscending 过滤 Sheet2。 Again, quick google on that.再次,快速谷歌。 Will look something like:看起来像:

Columns("A:C").Sort key1:=Range("C2"), _
  order1:=xlAscending, header:=xlNo

.4) Since you know your lowest price will be in the top row, and you know the column, you can have a messagebox show up to display what's in that cell: .4) 由于您知道最低价格将在顶行,并且您知道列,因此您可以显示一个消息框以显示该单元格中的内容:

MsgBox "Lowest price: "&Cells(1,4)

That should get you ready to code up what you want, in VBA.这应该让您准备好在 VBA 中编写您想要的代码。

`Private Sub SearchCommandButton_Click()
`Dim searchitem As Variant
`Dim lr As Long
`Dim WSNew As Worksheet
`Dim sheetname As String

`Set searchitem = SearchUserForm.TextBox1.Value
`lr = Cells(Sheets("GC").Rows.Count, 1).End(xlUp).Row
`For i = 1 To lr
`If IsError(WorksheetFunction.Match(searchitem, Cells(i, 2)), 0) > 0 Then
`Sheets("GC").Row(i).Copy
`Else
`Set WSNew = Worksheets.Add(After:=Sheets(ActiveSheet.Index))

    sheetname = searchitem

    On Error Resume Next

    WSNew.Name = sheetname
    If Err.Number > 0 Then
        MsgBox "We cannot match the search: " & WSNew.Name & _
             " Please try again" & _
             " Sheet already exist!" & _
             " The sheet name cannot contain this!"
        Err.Clear
    End If
    On Error GoTo 0

    With WSNew.Range("A1")

        .PasteSpecial Paste:=8
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
        Application.CutCopyMode = False
        .Select
    End With

End If

End Sub `结束子`

I tried another coding.我尝试了另一种编码。 This one is identifying the text i'm looking for a copy and paste to an existing sheet.这是识别我正在寻找的文本,复制并粘贴到现有工作表中。 Clear contents at the beginning of the macro.清除宏开头的内容。

`Private Sub SearchCommandButton_Click()
Dim rFind As Range
Dim rCopy As Range
Dim strSearch As String
Dim sFirstAddress As String
Dim destsh As Worksheet

Sheets("comparelist").Activate
Sheets("comparelist").Range("A2:AA200").ClearContents
strSearch = TextBox1.Value
Set rCopy = Nothing

Application.ScreenUpdating = False

With Sheets("GC").Columns("A:A")
Set rFind = .Find(strSearch, LookIn:=xlValues, Lookat:=xlPart,SearchDirection:=xlNext, MatchCase:=False)
If Not rFind Is Nothing Then sFirstAddress = rFind.Address
    Do
        If rCopy Is Nothing Then
            Set rCopy = rFind
        Else
            Set rCopy = Application.Union(rCopy, rFind)
        End If
        Set rFind = .FindNext(rFind)
    Loop While Not rFind Is Nothing And rFind.Address <> sFirstAddress

    rCopy.EntireRow.Copy
    Sheets("comparelist").Activate
    Sheets("comparelist").Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Application.ScreenUpdating = True
Unload Me
Sheets("comparelist").Range("A1").Select

End If
End With
End Sub  

What i'm trying to do next, is to compare values from columns D, I, N and R, the lowest to go yellow and the biggest value to go red, for each item.我接下来要做的是比较 D、I、N 和 R 列中的值,最低值变为黄色,最大值变为红色,对于每个项目。 Can anybody help?有人可以帮忙吗?

Many thanks!非常感谢! N. N。

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

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