2015-07-21 34 views
1

我的代碼當前運行通過第一列並找到某個關鍵字。我想用另一個關鍵字在下一列中進行另一次搜索,但僅限於第一列中找到該單詞的行。我想知道我該如何做到這一點。如何設置範圍到特定單元格

這裏是我到目前爲止的代碼:

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返回成爲下一列的搜索範圍。

+0

這是一個很好的問題,但答案並不明顯。我建議你閱讀這篇文章:http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/ 我有一個稍微不同的版本,強烈啓發了我可以給你的錫德,但我不確定我是否理解你的問題的第一句話。如果你澄清它會有所幫助! (關於您要搜索的順序) –

+0

@DavidGM鏈接幫助巨大。我已經對我的代碼進行了相應的更改,但是我現在被其他東西卡住了。通過直接信息與你聯繫會更容易,還是我應該在這裏重新編輯我的帖子? – user3242614

+0

重新編輯,也許別人可以幫助你!除非它沒有聯繫,否則提出一個新問題。 –

回答

0

我不確定你在哪裏坐在這個解決方案上,因爲它聽起來像問題已經隨着時間的推移而演變。根據我所理解的問題,您希望找到列A中與您的搜索詞匹配的所有行,然後僅搜索列B中的那些行以獲取不同的搜索詞。因此,舉例來說:

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

如果2個檢索詞是「蘋果」和「派」,它會返回第4行

我將與集合對象做到這一點。

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個獨立的搜索詞「蘋果」,「丿」,「簡」和「古道爾」返回一行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 
+0

嗨,我想知道你是否可以幫助我處理另一個問題。我有「櫻桃鞋匠喬安娜」。我怎樣才能將這一行數據插入第2行。因此,我將前三列中的每一列都存儲到一個集合中,但我不確定在最後一列確定將其插入到第2行上方時可以執行什麼檢查。 – user3242614

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 - 我看到你正在按行搜索。你需要這樣做,或者是上面的好嗎?

+0

您好我現在改變了我的問題,我已經解決了這個問題。我決定在Excel中使用查找功能 – user3242614

0

@ user3242614說:

您好,我想知道如果你能幫助我的另一個問題。我有「櫻桃 補鞋匠喬安娜」。我怎麼能將這行數據插入第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的值。如果它爲零,他們已經「搜索」了一些不存在的東西,所以應該添加它。

因此,第一個集合收集後,我們會插入此代碼:

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 

心連心

相關問題