excel 对字符串中的重复单词进行计数,如果出现次数少于10次,则将其删除

k4aesqcs  于 2023-02-25  发布在  其他
关注(0)|答案(3)|浏览(159)

我是VBA新手,我写了一个代码来删除字符串中的重复单词。但是我希望能够计算字符串中单词的出现次数,如果出现次数少于10次,我希望删除该单词。
这是我的代码:

Function RemoveDupeWords(text As String, Optional delimiter As String = " ") As String

    Dim dictionary  As Object
    Dim i, part
    
    Set dictionary = CreateObject("Scripting.Dictionary")
    dictionary.comparemode = vbTextCompare
    
    For Each i In Split(text, delimiter)
        part = Trim(i)
        If part <> "" And Not dictionary.Exists(part) Then
            dictionary.Add part, Nothing
        End If
    Next
    
    If dictionary.Count > 0 Then
        RemoveDupeWords = Join(dictionary.keys, delimiter)
    Else
        RemoveDupeWords = ""
    End If
    
    Set dictionary = Nothing
End Function

我该怎么做呢?
我还没有真正尝试过任何东西,因为我不知道从哪里开始

2o7dmzc5

2o7dmzc51#

没有一些示例数据很难回答这个问题,我只能想象你想要Assert你想要维护的子字符串保持在它们原来的位置,你可以创建一个函数,但是如果你愿意使用ms365,尝试一下:

C1中的公式:

=LET(del," ",MAP(A1:A4,LAMBDA(x,LET(y,TEXTSPLIT(x,del),TEXTJOIN(del,,MAP(y,LAMBDA(z,IF(SUM(--(y=z))>9,z,""))))))))
vlju58qv

vlju58qv2#

不要将字典值设置为Nothing,而是使用它来存储一个计数,并在找到更多相同单词时递增:

For Each i In Split(text, delimiter)
        part = Trim(i)
        If part <> "" Then
            'if it IS NOT in the dictionary, add it. 
            If Not dictionary.Exists(part) Then
                dictionary.Add part, 1
            'if it IS in the dictionary, increment the value
            Else
                dictionary(part) = dictionary(part) + 1
            End If
        End If
    Next

现在您已经有了每个非空术语的计数,可以迭代字典并删除满足条件的任何内容:

For Each dictKey in dictionary.keys()
        if dictionary(dictKey) < 10 Then
            dictionary.remove(dictKey)
        End If
    Next dictKey

请注意,我还没有测试这一点,这是很长一段时间以来,我写的VBA,但我相信语法是正确的。
更新:
我远程连接到一台Windows笔记本电脑,快速运行了一下,并取得了成功:

您可以看到,只有单词atest如预期的那样通过了字典。
测试中使用的代码:

Dim dictionary  As Object
Dim i, part

Set dictionary = CreateObject("Scripting.Dictionary")
dictionary.comparemode = vbTextCompare

For Each i In Split("this is is is is is a a a a a a a a a a a a a a test test test test test test test test test test test test of of this funcitonality", " ")
    part = Trim(i)
    If part <> "" Then
        'if it IS NOT in the dictionary, add it.
        If Not dictionary.Exists(part) Then
            dictionary.Add part, 1
        'if it IS in the dictionary, increment the value
        Else
            dictionary(part) = dictionary(part) + 1
        End If
    End If
Next

For Each dictKey In dictionary.keys()
    If dictionary(dictKey) < 10 Then
        dictionary.Remove (dictKey)
    End If
Next dictKey
6tqwzwtp

6tqwzwtp3#

如果您希望计算子字符串在字符串中出现的次数,可以使用下面的函数

Public Function Count(byref ipHost as string, byref ipFind as string)
    Count = (len(iphost) - len(Replace(ipHost,ipFind,vbnullstring)))/len(ipFind)
end function

相关问题