2014-11-20 108 views
-1

我有一個範圍,包含不同列中的不同詞,範圍將是DT21:EH400。我想知道是否有一種方法,通過該範圍進行搜索,如果有一個字複製並粘貼到同一行也已開啓,但在B列在vba範圍內尋找文本

+0

是它是更多鈔票。你需要什麼? – 2014-11-20 22:51:06

+0

是的我正在尋找vba中的代碼,這將做到這一點。 – user3562334 2014-11-20 22:52:32

+0

查看本頁右側中間的*相關*問題,這些問題已經確定爲您的問題的解決方案。至少有一個鏈接似乎回答你的問題。如果您嘗試使用答案來回答您的問題,但遇到麻煩,請在此處編輯您的帖子以顯示您有TRIED的內容,我們很樂意提供幫助。 – Jeeped 2014-11-20 23:09:18

回答

-1

下面的代碼可能工作

它搜索範圍內的字,並將搜索詞爲其中搜索詞發現

Dim strSearch as String 
Dim rngData as Range 
Dim rngCell as Range 

strSearch = "Word to Search" 
Set rngData = Range(Cells(21,124),Cells(400,138)) 

For each rngCell in rngData 
    If rngCell.value = strSearch Then 
     cells(rngcell.Row, 2).Value = strSearch 
    End if 
Next rngCell 
+0

在循環之前設置rngData可能是個好主意嗎? – barryleajo 2014-11-20 23:24:34

+1

如果您打算檢查每個單元格並連接到「單元格」(,2),多個匹配項不需要,則逐行搜索可能更有效。例如'如果不是iserror(application.match(strSearch,cells(rw,124).resize(1,15),0)),那麼cells(rw,2)= strSearch' – Jeeped 2014-11-21 01:08:42

+0

Jeeped,我同意。尋找單個匹配是一種更有效的方法,而不是循環遍歷每個單元格。 – 2014-11-21 02:40:18

0

在該行的B列的值在查找功能內置應該比自己寫循環更快:

Sub findUsingFIND() 

    Dim searchString As String 
    searchString = Excel.Application.InputBox("Enter string please") 

    Dim targetArea As Range 
    Set targetArea = Excel.Application.InputBox("Select range to search", , , , , , , 8) 

    targetArea.Select 
    'Excel.ThisWorkbook.Sheets(1).Range("DT21:EH400").Select 

    Dim foundRange As Range 
    With targetArea 
     Set foundRange = _ 
      .Find(What:=searchString, _ 
        After:=.Cells(1), _ 
        LookIn:=xlValues, _ 
        LookAt:=xlWhole, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlNext, _ 
        MatchCase:=False) 
    End With 

    If Not foundRange Is Nothing Then 
     ThisWorkbook.Sheets(1).Range("B" & foundRange.Row) = searchString 
    Else 
     MsgBox "Nothing found" 
    End If 

End Sub 

如果有字符串,然後上面可以適應於以下幾種情況:

Sub findSeveralUsingFIND() 

    Dim searchString As String 
    searchString = Excel.Application.InputBox("Enter string please") 

    Dim targetArea As Range 
    Set targetArea = Excel.Application.InputBox("Select range to search", , , , , , , 8) 

    targetArea.Select 
    'Excel.ThisWorkbook.Sheets(1).Range("DT21:EH400").Select 

    Dim foundRange As Range 
    With targetArea 
     Set foundRange = _ 
      .Find(What:=searchString, _ 
        After:=.Cells(1), _ 
        LookIn:=xlValues, _ 
        LookAt:=xlWhole, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlNext, _ 
        MatchCase:=False) 

     If Not foundRange Is Nothing Then 
      FirstAddress = foundRange.Address 
      Do 
       ThisWorkbook.Sheets(1).Range("B" & foundRange.Row).Value = searchString 
       Set foundRange = .FindNext(foundRange) 
      Loop While Not foundRange Is Nothing And foundRange.Address <> FirstAddress 
     Else 
      MsgBox "Nothing found" 
     End If 

    End With 

End Sub 

由於@ChrisNeilsen指出有一個,如果你需要執行搜索很多,這將是最好非常快的方法倍。這使用數組。我的理解相對較淺,爲什麼這種方法很快,但我相信它與數組將數據存儲在彼此相鄰的一組內存地址中的方式相關。下面是不同的方法良好的比較:

http://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/

這裏使用的變體類型的數組的宏:

Sub findUsingVARARRAY() 

    Dim vArr As Variant, vRes As Variant 
    Dim j As Long 
    Dim n As Long 

    Dim searchString As String 
    searchString = Excel.Application.InputBox("Enter string please") 

    Dim targetArea As Range 
    Set targetArea = _ 
     Excel.Application.InputBox(prompt:="Select range to search", Type:=8) 

    Dim firstRow As Long 

    vArr = targetArea.Value2 
    ReDim vRes(LBound(vArr, 1) To UBound(vArr, 1), 1 To 1) 
    Dim r As Long, c As Long 
    For r = LBound(vArr, 1) To UBound(vArr, 1) 
    For c = LBound(vArr, 2) To UBound(vArr, 2) 
     ' use vbTextCompare for case insenstitive comapre 
     ' use vbBinaryCompare for case senstitive comapre 
     If StrComp(vArr(r, c), searchString, vbTextCompare) = 0 Then 
      vRes(r, 1) = searchString 
      Exit For 
     End If 
    Next c, r 

    targetArea.EntireRow.Columns(2) = vRes 
End Sub 
+1

小心你所假設的。一個結構良好的'Variant Array'循環可以比'Find'方法快得多(取決於數據) – 2014-11-21 05:45:28

+0

@chrisneilsen提高了您的評論 - 完全同意,我將編輯我的答案。奇怪的是我今天早上想起了這件事。 – whytheq 2014-11-21 10:07:41

+0

@chrisneilsen - 我編輯了我的嘗試。我猜測用戶不能處理那些非常大的範圍,因此需要使用數組。我不使用數組,所以如果我沒有儘可能地從他們那裏獲得更多的性能收益,請隨時來幫助我。 – whytheq 2014-11-21 11:27:37