问题
My code currently runs through the first column and finds a certain key word. I want to do another search in the next column with another key word but only for the rows in which the word was found in the first column. I was wondering how I could do this.
Here's my code so far:
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
Basically all the locations returned in FoundAt
become the search range for the next column.
回答1:
I'm not sure where exactly you're sitting on a solution to this since it sounds like the question has evolved over time. Based on what I understand the question to be, you want to find all the rows in column A that match your search term and then ONLY search those rows in column B for a different search term. So, for example:
A B
Apple Cobbler
Cherry Cobbler
Peach Pie
Apple Pie
Apple iPad
If your 2 search terms were "Apple" and "Pie", it would return row 4.
I would accomplish this with a Collection object.
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
Of course, this also presupposes that Apple Pie does not appear twice. Eg:
A B
Apple Cobbler
Cherry Cobbler
Peach Pie
Apple Pie
Apple iPad
Apple Pie
Will break this code.
Edit: Response to the comment
So now you want to search something like this?
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
And be able to return row 4 when you have 4 separate search terms "Apple", "Pie", "Jane", and "Goodall"?
At this point you may want to think about restructuring your data! J/K :D
So, instead of adding the value of the cell to the Collection object, we'll add a Row to the Collection object. Then we have to keep looping through our Collection until we have the Highlander (There can be only one!) In essence, we keep juggling back and fourth sifting out the bad data until we are left with only the good. I'm sure there's a way to do this recursively, but its 5 o'clock.
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:
How's this:
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
This might have a snag or two, depending on how your worksheet is set up. Just let me know if this works, or what errors you get if not. Also, any questions just ask!
Edit: Bah - I see that you're searching by rows. Do you need that to be the case, or is the above okay?
回答3:
@user3242614 said:
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.
So if you have the data set:
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
And someone enters:
Cherry Cobbler Joe Anna
You want to search for it and enter it alphabetically?
If that's the case, then after each For x = 1 to collection.Count .... Next
Loop, check the value of collection.Count. If its zero, they have "searched" for something that doesn't exist and so it should be added.
So after the first Collection is gathered, we would insert this code:
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
来源:https://stackoverflow.com/questions/31545420/how-to-set-range-to-specific-cells