计算单元格中的大写单词和标点符号

时间:2014-12-17 06:19:56

标签: excel excel-vba excel-formula vba

我已经使用这个网站很长时间来解决我的Excel问题但是我们无法找到解决以下问题的方法。 我在单元格B16到B936中有文本字符串(范围从大约5个字到1000个字长)。 我想实现两件事:

  1. 为每个单元格计算大写字母中单词的出现次数 超过两个字符。所以基本上"I find that StackOverflow is a really GREAT website"会返回得分"1" 因为"GREAT"是此文本字符串中唯一更长的单词 超过2个字符和大写字母

  2. 计算多个问号("??", "???", "????", ...)出现在这些文本字符串中的次数。例如:"Are you sure ?? Really sure ???" would return the score "2"。我试过用一个 forumla (LEN - LEN(Substitute(..;"??"))但显然是这样的 公式遇到"????"它会返回奇怪的结果。

2 个答案:

答案 0 :(得分:0)

你在这里:

Function CountUppercaseWords(Expression As String) As Integer
    Dim SplitArray() As String
    SplitArray = Split(Expression, " ")
    Dim I As Integer
    Dim Count As Integer
    Dim NextWord As String
    Count = 0
    For I = LBound(SplitArray) To UBound(SplitArray)
        NextWord = SplitArray(I)
        If NextWord = UCase(NextWord) And Len(NextWord) >= 2 Then
            Count = Count + 1
        End If
    Next
    CountUppercaseWords = Count
End Function

Function MultipleQuestionMarkOccurences(Expression As String) As Integer
    Dim I As Integer
    Dim CurrentLength As Integer
    Dim Count As Integer
    Dim NextChar As String
    Count = 0
    CurrentLength = 0
    For I = 1 To Len(Expression)
        NextChar = Mid(Expression, I, 1)
        If NextChar = "?" Then
            CurrentLength = CurrentLength + 1
        Else
            CurrentLength = 0
        End If
        If CurrentLength = 2 Then
            Count = Count + 1
        End If
    Next
    MultipleQuestionMarkOccurences = Count
End Function

答案 1 :(得分:0)

这不是完美的解决方案,但可能有帮助。     公共函数CountUpperWords(ByVal String1 As String)As Integer

Dim StringArray() As String
ReDim StringArray(Len(String1)) As String
Dim String2 As String
Dim String2Array() As String
ReDim String2Array(UBound(StringArray)) As String
Dim isUpper As Boolean
String2 = UCase(String1)

For i = 0 To Len(String1) ' fragmentation all text to array and make UCase mirror 
    StringArray(i) = Mid(String1, i + 2, 1) 'Function would start from secound character because first character often will be upper
    String2Array(i) = LCase(StringArray(i))
Next

CountUpperCases = 0

For i = 0 To UBound(StringArray) ' compare original text and upper
    If StringArray(i) <> String2Array(i) Then
        j = i
        isUpper = False
        Do While StringArray(j) <> " " ' checking word is upper
            If StringArray(j) <> String2Array(j) Then
                isUpper = True
                Else
                isUpper = False
            End If
            j = j + 1
        Loop
        If isUpper = True Then
                CountUpperCases = CountUpperCases + 1
            End If
        i = j
    End If
Next

End Function