在电子表格的一列中如何突出显示重复单词,忽略文本大小写和标点符号。

huangapple go评论133阅读模式
英文:

How to highlight duplicate word in a column in spreadsheet, ignoring text case and Punctuation

问题

我正在在Excel上进行一个项目,但由于它是来自多个不同人的集体数据,我想在编译后找到任何重复的单词并将其突出显示。我找到了一个网站,它解释了如何使用宏来突出显示单元格中的重复单词,而且不区分大小写,还有一些叫做宏的东西?抱歉,我还是新手,但是否可以检测一列中的重复内容,而且不区分大小写,也不考虑标点符号如“.”或“/”?

我刚学会如何使用宏的功能。编辑代码仍然让我感到困惑。

英文:

I'm working on a project in excel, but since its a collective data from multiple different person, i want to find any duplicate word after compile it and highlight it. I found a site where it explain about how to Highlight duplicate words in a cell ignoring text case with thing called macro? sorry im new to this, but can it be possible to detect duplicate in one column, ignoring text case and punctuation like "." or "/"?

i just learn the function of macro. Editing the code still confusing.

答案1

得分: 0

使用Scripting.Dictionary来实现。

Sub How_to_highlight_duplicate_word_in_a_column_in_spreadsheet_ignoring_text_case_and_Punctuation()
    Dim cln As Range, ws As Worksheet, c As Range, cv As String, cCollection As New VBA.Collection, key, colr As Long
    Dim cDict As New Scripting.Dictionary
    'Dim cDict As Object
    'Set cDict = CreateObject("Scripting.Dictionary")
    Set ws = ActiveSheet
    Set cln = ws.UsedRange.Columns(3) 'for test
    For Each c In cln.Cells
        cv = c.Value
        If cv <> vbNullString Then
            cv = VBA.StrConv(cv, vbLowerCase)
            RemovePunctuation cv
            If cDict.Exists(cv) Then
                Set cCollection = cDict(cv)
                cCollection.Add c
            Else
                Set cCollection = Nothing
                cCollection.Add c
                cDict.Add cv, cCollection
            End If
        End If
    Next c

    For Each key In cDict.Keys
        Set cCollection = cDict(key)
        If cCollection.Count > 1 Then
            colr = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
            For Each c In cCollection
                ComplementaryColors c, colr
'                With c.Interior
'            '        .Pattern = xlSolid
'            '        .PatternColorIndex = xlAutomatic
'                    .Color = colr
'            '        .TintAndShade = 0
'            '        .PatternTintAndShade = 0
'                End With
            Next c
        End If
    Next key
End Sub

Function RemovePunctuation(str As String) As String
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Pattern = "[^A-Z0-9 ]"
        '.Pattern = "[^\w\s]"
        .IgnoreCase = True
        .Global = True
        str = .Replace(str, "")
    End With
    RemovePunctuation = str
End Function

Sub ComplementaryColors(cell As Range, color As Long)
    With cell.Font
        .color = color
'        .color = RGB(255 - color Mod 256, 255 - (color \ 256) Mod 256, 255 - color \ 65536)
    End With
    With cell.Interior
        .color = RGB(255 - color Mod 256, 255 - (color \ 256) Mod 256, 255 - color \ 65536)
    End With
    
End Sub

(Note: The code provided is in VBA and contains comments in English, which have not been translated.)

英文:

Use Scripting.Dictionary to implement.

Sub How_to_highlight_duplicate_word_in_a_column_in_spreadsheet_ignoring_text_case_and_Punctuation()
    Dim cln As Range, ws As Worksheet, c As Range, cv As String, cCollection As New VBA.Collection, key, colr As Long
    Dim cDict As New Scripting.Dictionary
    'Dim cDict As Object
    'Set cDict = CreateObject("Scripting.Dictionary")
    Set ws = ActiveSheet
    Set cln = ws.UsedRange.Columns(3) 'for test
    For Each c In cln.Cells
        cv = c.Value
        If cv <> vbNullString Then
            cv = VBA.StrConv(cv, vbLowerCase)
            RemovePunctuation cv
            If cDict.Exists(cv) Then
                Set cCollection = cDict(cv)
                cCollection.Add c
            Else
                Set cCollection = Nothing
                cCollection.Add c
                cDict.Add cv, cCollection
            End If
        End If
    Next c

    For Each key In cDict.Keys
        Set cCollection = cDict(key)
        If cCollection.Count > 1 Then
            colr = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
            For Each c In cCollection
                ComplementaryColors c, colr
'                With c.Interior
'            '        .Pattern = xlSolid
'            '        .PatternColorIndex = xlAutomatic
'                    .Color = colr
'            '        .TintAndShade = 0
'            '        .PatternTintAndShade = 0
'                End With
            Next c
        End If
    Next key
End Sub

Function RemovePunctuation(str As String) As String
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Pattern = "[^A-Z0-9 ]"
        '.Pattern = "[^\w\s]"
        .IgnoreCase = True
        .Global = True
        str = .Replace(str, "")
    End With
    RemovePunctuation = str
End Function

Sub ComplementaryColors(cell As Range, color As Long)
    With cell.Font
        .color = color
'        .color = RGB(255 - color Mod 256, 255 - (color \ 256) Mod 256, 255 - color \ 65536)
    End With
    With cell.Interior
        .color = RGB(255 - color Mod 256, 255 - (color \ 256) Mod 256, 255 - color \ 65536)
    End With
    
End Sub

huangapple
  • 本文由 发表于 2023年5月26日 08:35:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定