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

时间:2015-07-21 17:09:39

标签: excel vba excel-vba range

我的代码目前在第一列中运行并找到某个关键字。我想在下一列中使用另一个关键字进行另一次搜索,但仅针对在第一列中找到该单词的行。我想知道如何做到这一点。

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

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

3 个答案:

答案 0 :(得分:1)

这是怎么回事:

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

这可能会有一两个障碍,具体取决于工作表的设置方式。请告诉我这是否有效,或者如果没有,你会得到什么错误。此外,任何问题只是问!

编辑:Bah - 我看到你正在按行搜索。你是否需要那样,或者上面还好吗?

答案 1 :(得分:0)

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

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

如果您的2个搜索字词是“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

当然,这也预示着Apple Pie不会出现两次。例如:

  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”时,能够返回第4行吗?

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

因此,我们不会将单元格的值添加到Collection对象,而是向Collection对象添加一行。然后我们必须继续循环我们的集合,直到我们有汉兰达(只有一个!)本质上,我们继续玩杂耍,第四次筛选坏数据,直到我们只留下好处。我确信有一种方法可以递归地执行此操作,但是它的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

答案 2 :(得分:0)

@ user3242614说:

  

嗨,我想知道你是否可以帮我解决另一个问题。我有“樱桃   Cobbler Joe Anna“。我怎么能将这一行数据插入第2行。所以我   将前三列中的每一列都存储到一个集合中,但我不确定   检查我可以在最后一列做什么来确定将其插入第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的值。如果它为零,则它们“搜索”了不存在的东西,因此应该添加它。

所以在收集第一个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