如何将范围设置为特定单元格

how to set range to specific cells

我的代码目前遍历第一列并找到某个关键字。我想在下一列中使用另一个关键字进行另一次搜索,但仅限于在第一列中找到该词的行。我想知道我该怎么做。

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

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中返回的所有位置都成为下一列的搜索范围。

怎么样:

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

这可能会遇到一两个问题,具体取决于您的工作表的设置方式。请让我知道这是否可行,或者如果不可行会出现什么错误。还有,有什么问题尽管问!

编辑:呸——我看到你在按行搜索。你需要这样吗,或者以上是否可以?

我不确定您对这个问题的解决方案到底在哪里,因为听起来这个问题随着时间的推移而演变。根据我对问题的理解,您希望在 A 列中找到与您的搜索词匹配的所有行,然后仅在 B 列中搜索这些行以获得不同的搜索词。所以,例如:

  A          B
Apple     Cobbler
Cherry    Cobbler
Peach     Pie
Apple     Pie
Apple     iPad

如果您的 2 个搜索字词是 "Apple" 和 "Pie",它将 return 第 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

当然,这也以苹果派没有出现两次为前提。例如:

  A          B
Apple     Cobbler
Cherry    Cobbler
Peach     Pie
Apple     Pie
Apple     iPad
Apple     Pie

将破解此代码。


编辑:对评论的回复

所以现在你想搜索这样的东西?

  A          B        C        D
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 个单独的搜索词 "Apple"、"Pie"、"Jane" 和 "Goodall" 时,能够 return 第 4 行?

此时您可能需要考虑重组您的数据! J/K:D

因此,我们不会将单元格的值添加到 Collection 对象,而是将 Row 添加到 Collection 对象。然后我们必须不断循环我们的集合,直到我们拥有 Highlander(可能只有一个!)本质上,我们不断地反复来回筛选坏数据,直到我们只剩下好的数据。我确定有一种方法可以递归地执行此操作,但它是 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

@user3242614 说:

Hi, I was wondering if you could help me with another issue. I have "Cherry Cobbler Joe Anna". How could i insert this line of data into line 2. So i stored each of the first three columns into a collection, but I'm not sure what check I can do on the last column to determine to insert it above line 2.

所以如果你有数据集:

  A          B        C        D
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

您想搜索并按字母顺序输入吗?

如果是这样,那么在每个 For x = 1 to collection.Count .... Next 循环之后,检查 collection.Count 的值。如果它为零,他们有 "searched" 表示不存在的东西,因此应该添加它。

所以在收集到第一个 Collection 之后,我们将插入此代码:

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

hth