2016-12-12 84 views
0

Excel VBA中查找並返回結果所以我用這個功能,以便找到一個詞出現在任何時候都:符合多個條件

例:

=BusquedaSimple(Sheet1!B1:B6; "*"&ESPACIOS(B1)&"*"; 1) 

`sheet1` 
    A  B  C 
1 Basic Case1 Return1 
2 Basic6 Case1 Return2 
3 Basic Case1 Return3 
4 Basic2 Case2 Return4 
5 Basic Case2 Return5 
6 Basic5 Case3 Return6 

`sheet2` 
    A  B  C 
1 Basic Case1 =FUNCTION 
2 Basic Case2 =FUNCTION 
3 Basic2 Case3 =FUNCTION 

VBA功能:

Function BusquedaSimple(rng As Range, val As String, col As Long) 
Application.ScreenUpdating = False 
Dim i As Long, v, s 
Dim r As Long 

r = rng.Cells.count 
v = Application.Match(val, rng, 0) 
s = "" 
Do While Not IsError(v) 
    s = s & IIf(s <> "", Chr(10), "") & rng.Cells(v).Offset(0, col - 1).Value & ":" & rng.Cells(v).Offset(0, 0).Value 
    r = r - v 
    Set rng = rng.Offset(v, 0).Resize(r, 1) 
    v = Application.Match(val, rng, 0) 
Loop 
BusquedaSimple = s 
Application.ScreenUpdating = True 
End Function 

這工作正常,但知道,我需要匹配兩個標準,例如:

=BusquedaSimple((Sheet1!B1:B6; "*"&ESPACIOS(B1)&"*"); (Sheet1!A1:A6; "*"&ESPACIOS(A1)&"*"); 1) 

所以,我需要找到所有具有類似「案例1」出現在字符串的時代,可是,我需要確保該行也有像「基本」,一個字符串並存儲在一個單元(不是計數),但每個完整的字符串finded。

我該如何實現這一目標?

回答

0

我不確定你想做什麼,但我認爲你可以實現任何你想要的更容易。當使用VBA時,您不需要使用工作表功能。我建議使用這個函數來計算兩個給定字符串的出現次數。那麼你也許可以開發這種更得到你到底要:

Function CountOccurrences(rng As Range, str1 As String, str2 As String) As Integer 
    Dim cell As Range 
    Dim count As Integer 
    Dim WS As Worksheet 

    Set WS = ActiveSheet 
    count = 0 

    For Each cell In rag 
     If InStr(str1, cell.Value) > 0 And InStr(str2, cell.Value) > 0 Then 
      count = count + 1 
     End If 
    Next cell 

    CountOccurrences = count 
End Function 
+0

謝謝您的回答,事情是我必須在一個單元記錄所有的比賽,而不是數量。 – user3404207

+0

如果你能給我們一個輸入和輸出的例子,它會更容易找出你想要做什麼 – Ibo

0

難道你們就不能使用本地公式,像COUNTIFS,或者按照下面的陣列?

=SUM(--(LEFT(Sheet1!$A$2:$A$6,5)="Basic")*--(Sheet1!$B$2:$B$6="Case1"))+SUM(--(LEFT(Sheet2!$A$2:$A$6,5)="Basic")*--(Sheet2!$B$2:$B$6="Case1"))