检查列中的列是否具有类似的值

时间:2018-03-10 15:52:13

标签: excel vba excel-vba

我有下面提到的excel表:

not exists

我想检查这些列中的三个是否有任何单词(或单词的最大长度)与其他两列相似。

我想忽略这里的案例差异。

我有以下提到的VBA代码,但它无法提供所需的输出。

SELECT CustomerID, YEAR(OrderDate),COUNT(*)
FROM Sales.SalesOrderHeader oh
WHERE YEAR(OrderDate) IN (2011, 2014)
AND NOT EXISTS (SELECT 1
                FROM Sales.SalesOrderHeader oh1
                WHERE oh.CustomerID=oh1.CustomerID
                AND YEAR(oh1.OrderDate) IN (2012, 2013)
               )
GROUP BY CustomerID, YEAR(OrderDate)
HAVING COUNT(DISTINCT YEAR(OrderDate)) = 2

必需输出:

ID  column1    column2     column3
1   Apple X    apple - x   Apple - le
2   Mango Y    banana      cat
3   Dog        DOG - A     DOG - B
4   L-Lion     Lion        Lion- Cd

1 个答案:

答案 0 :(得分:1)

您可以尝试以下功能,但请注意它不适用于您的第四行,因为您将L-LION视为两个单词,但该功能会将其视为一个单词。您可以添加空格或考虑唯一分隔符。您也可以将函数扩展为第二个分隔符,如破折号。

Function isContained(ByVal s1 As String, ByVal s2 As String, ByVal s3 As String) As Boolean

Const BLANK = " "

Dim a As Variant

    a = Split(UCase(s1), BLANK)
    isContained = False

    Dim i As Long
    For i = LBound(a) To UBound(a)
        If InStr(1, s2, a(i), vbTextCompare) >= 1 And InStr(1, s3, a(i), vbTextCompare) >= 1 Then isContained = True
    Next i

End Function

编辑我添加了简短的“快速和脏”

Function isContained(ByVal s1 As String, ByVal s2 As String, ByVal s3 As String) As Boolean

Const BLANK = " "
Const DASH = "-"

Dim a As Variant

    a = Split(UCase(s1), BLANK)
    isContained = False

    Dim i As Long
    For i = LBound(a) To UBound(a)
        If InStr(1, s2, a(i), vbTextCompare) >= 1 And InStr(1, s3, a(i), vbTextCompare) >= 1 Then isContained = True
    Next i

    a = Split(UCase(s1), DASH)
    For i = LBound(a) To UBound(a)
        If InStr(1, s2, a(i), vbTextCompare) >= 1 And InStr(1, s3, a(i), vbTextCompare) >= 1 Then isContained = True
    Next i


End Function