类似的字符串VBA

时间:2015-02-23 14:45:35

标签: string vba excel-vba comparison similarity

我试图使用like运算符比较VBA中的类似字符串,但我无法做到。我希望该功能可以看到ESI临床操作和ESI商业定制/ HIX是相同的,因为它们都是从ESI开始的。但出于某种原因,它不会那样做。完成此任务的最佳选择是什么?在此先感谢!

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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