2014-02-18 25 views
0

我有一對可能有字符串的列,我想知道它們是哪一行。查找函數以查找多個字符串

Dim lx As Integer 
Dim FoundExec As Range 
With Worksheets("Load " & LoadNr(1)).Range("G10:G26") 
    Set FoundExec = .Find("Exec", LookIn:=xlValues) 
    If Not FoundExec Is Nothing Then 
     Dim FirstLoadAddress As String 
     FirstLoadAddress = FoundExec.Address 
     Do 
      lx = FoundExec.Row 
      Set FoundExec = .FindNext(FoundExec) 

      'Code... 

      Loop While FoundExec.Address <> FirstLoadAddress 
    End If 
End With 

這對我的作品,因爲它是應該的,但我想也找到值「OVS」,「OV」和「OS」,因爲相同的代碼是那些投入之後跟進。

我TREID

Dim lx As Integer 
Dim FoundExec As Range 
Dim FoundOVS As Range 
Dim FoundOV As Range 
Dim FoundOS As Range 
Dim AllOSS As Range 

With Worksheets("Load " & LoadNr(1)).Range("G10:G26") 
    Set FoundExec = .Find("Exec", LookIn:=xlValues) 
    Set FoundOVS = .Find("OVS", LookIn:=xlValues) 
    Set FoundOV = .Find("OV", LookIn:=xlValues) 
    Set FoundOS = .Find("OS", LookIn:=xlValues) 

    Set AllOSS = Application.Union(FoundExec, FoundOVS, FoundOV, FoundOS) 
    If Not AllOSS Is Nothing Then 
     Dim FirstLoadAddress As String 

     'Code... 

     FirstLoadAddress = AllOSS.Address 
     Do 
      lx = AllOSS .Row 
      Set AllOSS = .FindNext(AllOSS) 
      Loop While AllOSS.Address <> FirstLoadAddress 
    End If 
End With 

但是,輪流在不斷循環。

有沒有辦法快速做到這一點?爲什麼在正在進行的循環中轉換第二個?

+0

這個問題似乎因爲你不能「聯合」多次搜索,然後在其上執行'.FindNext'。在'Do'後面用'Debug.Print AllOSS.Address'檢查你的代碼,然後你就會明白爲什麼'Do Loop'永遠不會退出。 –

回答

1

忘記所有這些發現,查找非常,非常低效...

這樣做:

With Worksheets("Load " & LoadNr(1)).Range("G10:G26") 
nr = .Rows.Count 
For r = 1 To nr 
    If InStr("#Exec#OVS#OV#OS#", .Cells(r, 1) & "#") > 0 Then 
    ' do whatever you need to do 
    End If 
Next 
End With 

即使application.match比.Find更有效

+0

hehe ... with 4 .FINDS?這就是你認爲的嘗試,時間,然後說......我做到了,速度更快......更快,更快......(順便說一句,你認爲什麼?FIND正在做?) – CRondao

+0

** + 1 **實現**或**的好方法 –

+0

它不是用VBA編碼的,即使這樣更快,它也有這樣的機會。 –