2015-02-23 152 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