Excel 在不同的单元格中查找相同的单词

Excel 在不同的单元格中查找相同的单词,excel,vba,excel-formula,Excel,Vba,Excel Formula,在Excel中查找书籍标题中的常用词,输出如下: 'book common user_id physics physics 1 Principles of plasma physics physics,plasma 2 Fun

在Excel中查找书籍标题中的常用词,输出如下:

   'book                              common                      user_id
    physics                           physics                         1
    Principles of plasma physics      physics,plasma                  2
    Fundamentals of plasma physics    fundamentals,plasma,physics     3
    Fundamentals of thermodynamics    fundamentals                    4
   '

这是我解决这个问题的办法。我知道代码相当混乱:我在变量名、错误处理等方面做得非常草率,但它让您了解了如何做到这一点。我创建了一个UDF
Common()
,它包含4个参数:

  • rngText:对单个单元格的引用,该单元格包含您要保存的文本(在案例手册中)
  • compareList:用于比较第一个参数的单元格范围
  • 最小出现次数(可选):这是一个单词应被视为“常见”的最小出现次数的定义。默认值为2
  • 排除列表(可选):包含应排除文本的单元格范围(例如“a”、“of”、…)
例如,如果你的标题在A2:A7中,排除列表在E2:E3中,你可以在单元格B2中使用公式
=Common(A2,$A$2:$A$7,$E$2:$E$3)
,然后复制到B7

Option Explicit

Function Common(rngText As Range, compareList As Range, _
    Optional minOccurences As Integer = 2, Optional exclusionList As Range) As Variant

    'Check if an exclusion list is provided
    Dim exclusionListProvided As Boolean
    If Not (exclusionList Is Nothing) Then
        exclusionListProvided = True
    Else
        exclusionListProvided = False
    End If

    'Check the argments
    Dim returnError As Boolean
    If IsDate(rngText.Value) Or IsNumeric(rngText.Value) Or IsError(rngText.Value) Then 'first argument should refer to a cell containing text
       returnError = True
    ElseIf minOccurences < 2 Then   'Function should check for at least 2 occurences
        returnError = True
    ElseIf (compareList.Columns.Count > 1 And compareList.Rows.Count > 1) Then  'compareList should be one-dimensional
        returnError = True
    ElseIf exclusionListProvided Then
        If (exclusionList.Columns.Count > 1 And exclusionList.Rows.Count > 1) Then  'exclusionList should be one-dimensional
            returnError = True
        End If
    Else
        returnError = False
    End If

    'Return an error if one of the arguments is unexpected
    If returnError Then
        Common = CVErr(xlErrValue)
    Else
        Dim text As String
        text = rngText.Value

        'split text into an array of words
        Dim words() As String
        words = fullSplit(text)

        'convert exclusionlist and compareList to arrays
        Dim arrExclude()
        If exclusionListProvided Then
            arrExclude() = rangeToStringArray(exclusionList)
        End If
        Dim arrCompare()
        arrCompare() = rangeToStringArray(compareList)

        Dim strCommon As String
        'loop through words in text
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim nOccurences As Integer
        Dim excluded As Boolean
        Dim compareWords() As String

        For i = LBound(words) To UBound(words)
            'check if word is in exclusion list
            excluded = False
            If exclusionListProvided Then
                For j = LBound(arrExclude) To UBound(arrExclude)
                    compareWords = fullSplit(arrExclude(j))
                    For k = LBound(compareWords) To UBound(compareWords)
                        If compareWords(k) = words(i) Then
                            excluded = True
                            Exit For
                        End If
                    Next k
                    If excluded Then Exit For
                Next j
            End If

            'count the number of occurences of the word in the compare list
            If Not excluded Then
                nOccurences = 0
                For j = LBound(arrCompare) To UBound(arrCompare)
                    compareWords = fullSplit(arrCompare(j))
                    For k = LBound(compareWords) To UBound(compareWords)
                        If LCase(compareWords(k)) = LCase(words(i)) Then
                            nOccurences = nOccurences + 1
                            Exit For
                        End If
                    Next k
                Next j
                If nOccurences >= minOccurences Then
                    If Not strCommon = "" Then
                        strCommon = strCommon & ", "
                    End If
                    strCommon = strCommon & LCase(words(i))
                End If
            End If
        Next i

        Common = strCommon
    End If


End Function

'split text by using a list of delimiters
Function fullSplit(text As Variant)
    'define list of delimiters
    Dim delimiters()
    delimiters = Array(" ", ",", ".", ";", "?", "!")

    'unique delimiter is the first one from the list
    Dim uniqueDelimiter As String
    uniqueDelimiter = delimiters(0)

    'replace all delimiters in the text by the unique delimiter
    Dim i As Integer
    For i = LBound(delimiters) + 1 To UBound(delimiters)
        Replace text, delimiters(i), uniqueDelimiter
    Next i

    'split the text by using the unique delimiter
    fullSplit = SplitText(text, uniqueDelimiter)

End Function

'split text by using a single delimiter
Function SplitText(text As Variant, delimiter As String)
    'split the text in substrings on each occurence of the delimiter
    Dim tempArray() As String
    tempArray = Split(text, delimiter)

    'remove empty substrings
    Dim LastNonEmpty As Integer
    LastNonEmpty = -1
    Dim i As Integer
    For i = LBound(tempArray) To UBound(tempArray)
        If tempArray(i) <> "" Then
            LastNonEmpty = LastNonEmpty + 1
            tempArray(LastNonEmpty) = tempArray(i)
        End If
    Next
    ReDim Preserve tempArray(0 To LastNonEmpty)

    SplitText = tempArray
End Function

'check if two arrays share a least one element
Function sharedElements(array1() As Variant, array2() As Variant) As Boolean
    Dim found As Boolean
    found = False

    Dim i As Integer
    Dim j As Integer
    For i = LBound(array1) To UBound(array1)
        For j = LBound(array2) To UBound(array2)
            If array1(i) = array2(j) Then
                found = True
                Exit For
            End If
        Next j
        If found = True Then Exit For
    Next i

    sharedElements = found
End Function

'converts a range to an array of strings, omitting all non-text cells
Function rangeToStringArray(myRange As Range)
    Dim myArray()
    Dim arraySize As Integer
    arraySize = 0

    Dim c As Object
    For Each c In myRange
        If IsDate(c.Value) = False And IsNumeric(c.Value) = False And IsError(c.Value) = False Then
            ReDim Preserve myArray(arraySize)
            myArray(arraySize) = c.Value
            arraySize = arraySize + 1
        End If
    Next

    rangeToStringArray = myArray
End Function
选项显式
功能通用(rngText作为范围,compareList作为范围_
可选最小发生率为整数=2,可选排除列表为范围)为变量
'检查是否提供了排除列表
以布尔形式提供
如果不是(排除列表什么都不是),那么
ExclutionListProvided=True
其他的
ExclutionListProvided=False
如果结束
“检查这些论点
将返回错误设置为布尔值
如果IsDate(rngText.Value)或IsNumeric(rngText.Value)或IsError(rngText.Value),则“第一个参数应引用包含文本的单元格
returnError=True
否则,如果最小发生次数<2,则“Then”函数应检查至少2次发生
returnError=True
ElseIf(compareList.Columns.Count>1和compareList.Rows.Count>1)则“compareList应该是一维的
returnError=True
艾尔塞夫:那么
如果(ExclutionList.Columns.Count>1和ExclutionList.Rows.Count>1),则“ExclutionList”应该是一维的
returnError=True
如果结束
其他的
returnError=False
如果结束
'如果其中一个参数意外,则返回错误
如果返回错误,那么
公共=CVErr(XLERR值)
其他的
将文本变暗为字符串
text=rngText.Value
'将文本拆分为单词数组
Dim words()作为字符串
words=fullSplit(文本)
'将排除列表和比较列表转换为数组
暗arrExclude()
如果有,那么
arExclude()=rangeToStringArray(排除列表)
如果结束
Dim arrCompare()
arrCompare()=rangeToStringArray(比较列表)
Dim strCommon As字符串
'循环浏览文本中的单词
作为整数的Dim i
作为整数的Dim j
将k变为整数
作为整数的维数
作为布尔值排除
将compareWords()设置为字符串
对于i=LBound(单词)到UBound(单词)
'检查单词是否在排除列表中
排除=错误
如果有,那么
对于j=LBound(arrExclude)到UBound(arrExclude)
compareWords=fullSplit(ArreExclude(j))
对于k=LBound(compareWords)到UBound(compareWords)
如果compareWords(k)=单词(i),则
排除=真
退出
如果结束
下一个k
如果排除,则退出
下一个j
如果结束
'计算比较列表中出现的单词数
如果不排除在外,则
无治愈率=0
对于j=LBound(arrCompare)到UBound(arrCompare)
compareWords=fullSplit(arrCompare(j))
对于k=LBound(compareWords)到UBound(compareWords)
如果LCase(compareWords(k))=LCase(单词(i)),则
无治愈率=无治愈率+1
退出
如果结束
下一个k
下一个j
如果无发生>=微小发生,则
如果不是strCommon=“”,则
strCommon=strCommon&“
如果结束
strCommon=strCommon&LCase(字(一))
如果结束
如果结束
接下来我
公共=strCommon
如果结束
端函数
'使用分隔符列表拆分文本
函数fullSplit(文本作为变量)
'定义分隔符列表
Dim分隔符()
分隔符=数组(“,”,“,”,“;”,“?”,“!”)
'唯一分隔符是列表中的第一个分隔符
Dim uniqueDelimiter作为字符串
唯一分隔符=分隔符(0)
'用唯一分隔符替换文本中的所有分隔符
作为整数的Dim i
对于i=LBound(分隔符)+1到UBound(分隔符)
替换文本、分隔符(i)、唯一分隔符
接下来我
'使用唯一分隔符拆分文本
fullSplit=SplitText(文本,唯一分隔符)
端函数
'使用单个分隔符拆分文本
函数SplitText(文本作为变量,分隔符作为字符串)
'在每次出现分隔符时将文本拆分为子字符串
Dim tempArray()作为字符串
tempArray=Split(文本,分隔符)
'删除空子字符串
Dim lastnempty作为整数
LastNonEmpty=-1
作为整数的Dim i
对于i=LBound(tempArray)到UBound(tempArray)
如果临时数组(i)“,则
LastNoneEmpty=LastNoneEmpty+1
tempArray(LastNonEmpty)=tempArray(i)
如果结束
下一个
ReDim保留临时数组(0到LastNoneEmpty)
SplitText=tempArray
端函数
'检查两个数组是否共享至少一个元素
函数sharedElements(array1()作为变量,array2()作为变量)作为布尔值
他被认为是傻瓜