【问题标题】:Counting uppercase words and punctuation in a cell计算单元格中的大写单词和标点符号
【发布时间】:2014-12-17 17:52:46
【问题描述】:

我长期使用这个网站来解决我的 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(..;"??")) 但显然当这个 公式遇到"????" 它返回一个奇数结果。

【问题讨论】:

  • 我觉得这很搞笑。我很想知道您使用这些数据进行的实际报告的结果。
  • 当您返回结果时布局会是什么样子? 1)去“C” 2)去“D”?
  • 您能否给出该数据列中可能出现的所有标点符号的明确列表?
  • “我已经使用这个网站很长时间来解决我的 Excel 问题”,但这是您的第一个问题,并且您是会员,因为...今天。而且您不提供代码或尝试。
  • @PJ Rosenburg :大写字母和多个标点符号被用作写作中情绪(主要是负面情绪)的代理。现有的元范数计算效价和基于单词和其他工件的交互没有考虑在内。

标签: excel excel-formula vba


【解决方案1】:

你在这里:

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

【讨论】:

  • 我不是 VBA 专家,但请参阅我对上述 OP 的问题重新标点符号。例如,此解决方案是否也会忽略字符串“IS!” - 我认为不应将其视为计数的一部分 - 来自诸如“您知道任何伟大的网站吗?是的!StackOverflow IS!”之类的案例?
  • 是的,你是对的。首先,您必须从 Expression 中删除所有标点符号。还要定义标点符号。
  • @ XOR LX:好点。谢谢你。正在对法语文本进行分析,任何超过 2 个大写字符的内容都值得关注。您提供的示例实际上并不适用于法语。它是英语中常见的东西吗?它的含义是什么? “IS!”是否有任何情感意义? ?
  • 他的意思是我的方法将任何不是空格的字符计算为单词的一部分。
  • 不客气。如果您当时接受我的回答,我将不胜感激。
【解决方案2】:

这不是完美的解决方案,但可能会有所帮助。 公共函数 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多