如何设置范围到特定的单元格

我的代码当前运行通过第一列,并find一个特定的关键字。 我想在另一个关键字的下一列进行另一个search,但仅限于在第一列中find该单词的行。 我想知道如何做到这一点。

这是我的代码到目前为止:

Set aCell = oRange.Find(What:=firstInput, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then Set bCell = aCell FoundAt = aCell.Address Do Set aCell = oRange.FindNext(After:=aCell) If Not aCell Is Nothing Then If aCell.Address = bCell.Address Then Exit Do FoundAt = FoundAt & ", " & aCell.Address Else Exit Do End If Loop Else MsgBox SearchString & " not Found" Exit Sub End If MsgBox "The Search String has been found these locations: " & FoundAt Exit Sub 

基本上, FoundAt返回的所有位置成为下一列的search范围。

我不确定你到底在哪里解决这个问题,因为听起来问题是随着时间的推移而变化的。 根据我所理解的问题,您希望find列A中与您的search词匹配的所有行,然后仅search列B中的那些行以获取不同的search词。 所以,例如:

  AB Apple Cobbler Cherry Cobbler Peach Pie Apple Pie Apple iPad 

如果你的2个search条件是“Apple”和“Pie”,它将返回第4行。

我会用一个Collection对象来完成这个任务。

 Dim myCol As New Collection Dim r As Integer Dim FirstSearchTerm As String Dim SecondSearchTerm As String For r = 1 To Sheet1.UsedRange.Rows.Count If Sheet1.Cells(r, 1) = FirstSearchTerm Then myCol.Add r, Sheet1.Cells(r, 2) End If Next MsgBox myCol.Item(SecondSearchTerm) 'Returns 4 

当然,这也预示着苹果派不会出现两次。 例如:

  AB Apple Cobbler Cherry Cobbler Peach Pie Apple Pie Apple iPad Apple Pie 

将打破这个代码。


编辑:对评论的回应

那么现在你想要search这样的东西?

  ABCD Apple Cobbler Joe Blow Cherry Cobbler Joe DiMaggio Peach Pie Sally Sparrow Apple Pie Jane Goodall Apple iPad Ivan Terrible Apple Pie Sally Sparrow Apple Pie Jane Seymour 

当你有4个单独的search词“苹果”,“馅饼”,“简”和“古道”时,能够返回第4行吗?

在这一点上,你可能想考虑重构你的数据! J / K:D

因此,我们不是将单元格的值添加到Collection对象,而是将一个Row添加到Collection对象。 然后,我们必须继续循环我们的collections,直到我们有汉兰达人(可以只有一个!)本质上,我们继续杂耍回来和第四次筛选出的坏数据,直到我们只剩下好的。 我相信有一种recursion的方法,但它的5点。

 Dim myFirstCol As New Collection Dim mySecCol As New Collection Dim x As Integer 'Switching to x so its not as confusing Dim FirstSearchTerm As String Dim SecondSearchTerm As String Dim ThirdTerm As String Dim FourthTerm As String FirstSearchTerm = "Apple" SecondSearchTerm = "Pie" ThirdTerm = "Jane" FourthTerm = "Seymour" 'First, get all rows matching our first term For x = 1 To Sheet1.UsedRange.Rows.Count If Sheet1.Cells(x, 1) = FirstSearchTerm Then myFirstCol.Add x 'Just save the row number End If Next 'now loop through that collection search for our second term 'Item(x) contains the row number for rows that matched our search terms For x = 1 To myFirstCol.Count If Sheet1.Cells(myFirstCol.Item(x), 2) = SecondSearchTerm Then mySecCol.Add myFirstCol.Item(x) End If Next 'And loop through again for the 3rd term Set myFirstCol = New Collection 'Reuse it For x = 1 To mySecCol.Count If Sheet1.Cells(mySecCol.Item(x), 3) = ThirdTerm Then myFirstCol.Add mySecCol.Item(x) End If Next 'And fourth Set mySecCol = New Collection 'Reuse it For x = 1 To myFirstCol.Count If Sheet1.Cells(myFirstCol.Item(x), 4) = FourthTerm Then mySecCol.Add myFirstCol.Item(x) End If Next msgbox mySecCol.Item(1) 'Which is the row number matching all our terms 

这个怎么样:

 Sub test2() Dim firstInput As String Dim i As Integer, col As Integer Dim Rng As Range Dim check1 As Boolean firstInput = "cat" col = 1 ' this will start us off in column 1 ("A") With Sheets("New") ' using this sheet ' There's no reason to loop through ALL columns in Excel, let's just use the columns that are actually in use (.usedrange.columns.count) For i = 1 To .UsedRange.Columns.Count Do While Rng Is Nothing 'This will loop until a match is found, or not found If col > .UsedRange.Columns.Count Then Exit Do ' if we exceed the used columns, you can stop looking through them Set Rng = .Columns(col).Find(what:=firstInput, LookIn:=xlValues, lookat:=xlWhole, searchOrder:=xlByRows) col = col + 1 Loop Next i If Rng Is Nothing Then MsgBox ("Nothing found in this sheet") Else ' When there is a match, do below code MsgBox (firstInput & " found in cell " & Rng.address) Application.Goto Rng, True RowNum = Rng.row MsgBox RowNum check1 = True End If End With End Sub 

这可能有一两个障碍,取决于你的工作表是如何设置的。 只要让我知道这是否工作,或者如果不是,你会得到什么样的错误。 另外,任何问题只是问!

编辑:巴 – 我看到你正在按行search。 你需要这样的情况,还是以上好吗?

@ user3242614说:

嗨,我想知道如果你能帮我解决另一个问题。 我有“樱桃鞋匠乔安娜”。 我怎么能插入这行数据到第2行。所以我将每个前三列存储到一个集合,但我不知道什么检查我可以在最后一列确定插入它上面第2行。

所以,如果你有数据集:

  ABCD Apple Cobbler Joe Blow Cherry Cobbler Joe DiMaggio Peach Pie Sally Sparrow Apple Pie Jane Goodall Apple iPad Ivan Terrible Apple Pie Sally Sparrow Apple Pie Jane Seymour 

有人进入:

 Cherry Cobbler Joe Anna 

你想要search并按字母顺序input?

如果是这样的话,那么在每个For x = 1 to collection.Count .... Next Loop,检查collection.Count的值。 如果它为零,他们已经“search”了一些不存在的东西,所以它应该被添加。

所以收集第一个集合之后,我们会插入这个代码:

 If myFirstCol.Count = 0 Then 'No values matched For r = 1 To Sheet1.UsedRange.Rows.Count 'But for subsequent loops you need to loop through the Collection Like this: ' For r = 1 To myFirstCol.Count If Sheet1.Cells(r, 1) > FirstSearchTerm Then r = r - 1 ' The Row the data should be inserted into. Rows(r).Insert Cells(r, 1) = FirstSearchTerm Cells(r, 2) = SecondSearchTerm Cells(r, 3) = ThirdTerm Cells(r, 4) = FourthTerm Exit For 'Jump out..no sense doing more comparisons End If Next End If 

心连心