0

我想比较两列的名称,但它们的格式不同,例如,一列是Xu, Boxi; 另一个是BOXI XU。所以我需要比较它忽略顺序和逗号而不区分大小写。

到目前为止,我可以通过在 Excel 中使用 UPPER 来解决区分大小写的部分,但无法解决顺序和逗号问题。这是我用于比较功能的 VBA 代码。

Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant, y As Variant

    ' Set CompareRange equal to the range to which you will
    ' compare the selection.
    Set CompareRange = Range("C1:C5")
    ' NOTE: If the compare range is located on another workbook
    ' or worksheet, use the following syntax.
    ' Set CompareRange = Workbooks("Book2"). _
    '   Worksheets("Sheet2").Range("C1:C5")
    '
    ' Loop through each cell in the selection and compare it to
    ' each cell in CompareRange.
    For Each x In Selection
        For Each y In CompareRange
            If x = y Then x.Offset(0, 1) = x
        Next y
    Next x   
End Sub

还有一点就是,如上图,循环只是 from C1 to C5,不知道结束索引的情况下,如何让它循环到图表结束呢?

4

1 回答 1

0

未经测试:

Sub Find_Matches()
    Dim CompareRange As Variant, x As range, y As range
    dim sht as worksheet, n

    set sht = activesheet

    Set CompareRange = sht.Range(sht.range("C1"), _
                                 sht.cells(rows.count, 3).end(xlup))


    For Each x In Selection.cells
        n = normalize(x.value) 
        For Each y In CompareRange.cells
            If n = normalize(y.value) Then 
                x.Offset(0, 1) = x
                exit for
            end if
        Next y
    Next x   
End Sub

function normalize(v) as string
dim a
     v=trim(ucase(v))
     a=split(v,",")
     're-arrange if has single comma...
     if ubound(v)=1 then v= trim(a(1)) & " " & trim(a(0))
     normalize = v
end function
于 2013-11-06T07:26:07.077 回答