2015-02-23 52 views
0

我想比較VBA中類似的字符串使用類似的運算符,但我不能。我希望該功能能夠看到ESI臨牀操作和ESI商業定製/ HIX是相同的,因爲它們都以ESI開始。但由於某種原因,它不會那樣做。什麼是我最好的選擇來完成這個?提前致謝!類似的字符串VBA

Function SetInternalClientID() 

Sheet9.Activate 

Columns("J:J").Select 
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 

Set rng2 = FindHeader("CLIENT NAME", Sheet9.Name) 

Count = 0 

For i = 73 To rng2.Rows.Count 

Pattern = Left(rng2.Cells(i - 1, 1).Value, 6) 

If Pattern = "Blue S" Or Pattern = "BCBS o" Then 
    Pattern = Right(rng2.Cells(i - 1, 1).Value, 7) 
ElseIf Pattern = "Health" Then 
    Pattern = Left(rng2.Cells(i - 1, 1).Value, 8) 
End If 

ClientCheck = rng2.Cells(i, 1).Value Like Pattern 

If ClientCheck = True Then 

MsgBox (rng2.Cells(i, 1) & " Like " & rng2.Cells(i - 1, 1).Value) 

Else 

MsgBox (rng2.Cells(i, 1) & " NOT LIKE " & rng2.Cells(i - 1, 1).Value & " " & Pattern) 

End If 

Next i 


End Function 
+0

你不能使用沒有模式的'Like'!你的模式@Philip是什麼? – 2015-02-23 15:28:07

+0

@JLILIAman在這種情況下,它將是ESI。我用我正在工作的功能編輯了我的問題。我使用子字符串函數來確定一個模式,但是我沒有得到任何比較長的字符串的匹配,例如上面提到的兩個字符串。我怎麼能解決這個問題? – Philip 2015-02-23 16:15:58

+0

rng2中是否有任何公式? – 2015-02-23 18:45:06

回答

0
Sub myTextCheck() 

    Dim txtOne As String, txtTwo As String 

    txtOne = "ESI Clinical Operations" 
    txtTwo = "ESI Commercial Custom/HIX" 

    If txtOne Like "ESI*" And txtTwo Like "ESI*" Then 

    Debug.Print "they both start with ESI" 

    Else 
     Debug.Print "not..." 

End If 

End Sub 
0

我加入一些代碼下面這應該幫助你前進的過程中。這是一個UDF,它需要參數string來檢查並將其與模式進行比較。代碼將模式分解爲單個單詞,並檢查是否在text_to_check中找到任何這些單詞。

我擔心它會返回誤報。很明顯,您正在製作醫療/保險電子表格。 (例如)「臨牀」這個詞是否可能在不同的行中重複多次? [Aetna Clinical,ESI Clinical等]更好地定義匹配條件可能是有益的。

Function Modified_Like(strTextToCheck, strPattern) As Boolean 
    Dim strArray() As String 
    Dim bFound As Boolean 
    strArray = Split(strPattern, " ") 

    bFound = False 

    For Each itm In strArray 
     If InStr(1, strTextToCheck, itm, 1) > 0 Then 
      bFound = True 
      Exit For 
     End If 
    Next itm 

    Modified_Like = bFound 
End Function