【问题标题】:Count number of characters of each word in a cell in excel计算excel单元格中每个单词的字符数
【发布时间】:2020-07-02 16:06:15
【问题描述】:

例如,如果我在一个单元格中有句子:

The fox jumped twice    

输出应该是

3 3 6 5

我尝试过将len()trim()函数组合使用,但还没有找到解决方案,担心只能使用VBA来完成。

【问题讨论】:

  • VBA 函数似乎是这个 imo 的最佳选择。
  • 不同单元格中可以有多少个单词(最多?)没有VBA也可以
  • @AlexK。你绝对是对的。
  • @Pierre44 通常为 3-4 个字,但最多可达 8 个。
  • 老线程,但是用普通功能比较容易做到这一点。看看this

标签: excel vba string count


【解决方案1】:
Function count_Chars(ByVal target As Range)
Dim splt

splt = Split(target, " ")

Dim i As Long
Dim output As String

For i = LBound(splt) To UBound(splt)
    output = output & " " & Len(splt(i))
Next i

count_Chars = TRIM(output)
End Function

【讨论】:

  • 非常感谢!节省了我很多时间
  • @DavidGorgan - 如多个答案所示,有几种方法可以做到这一点。无论哪一个适合您作为您的答案,您介意通过单击帖子左侧的复选标记来标记它吗?
【解决方案2】:

试试这个基于短数组的 UDF。

Option Explicit

Function wordLength(str As String)
    Dim i As Long, arr As Variant
    arr = Split(str, Chr(32))
    For i = LBound(arr) To UBound(arr)
        arr(i) = Len(arr(i))
    Next i
    wordLength = Join(arr, Chr(32))
End Function

【讨论】:

  • 只是好奇 - 为什么使用 Chr(32) 而不是 " "
  • 非常感谢!
【解决方案3】:

用户定义函数确实是个好主意:

Public Function CountWords(inString As String) As String

    Dim i As Long
    Dim myArr As Variant: myArr = Split(inString)
    For i = LBound(myArr) To UBound(myArr)
        CountWords = CountWords & " " & Len(myArr(i))
    Next i
    CountWords = Trim(CountWords)

End Function
  • Split() 不需要参数,如果是空格,则用什么分割;
  • Trim() 删除最后一个空格;

【讨论】:

    【解决方案4】:

    只是为了展示你在没有 VBA 的情况下得到的可怕答案:

    您可以查找“”,然后计算不同空格之间的长度。

    =find(" ",B3,1)-1&" "&
    find(" ",B3,find(" ",B3,1)+1)-(find(" ",B3,1))-1&" "&
    find(" ",B3,find(" ",B3,find(" ",B3,1)+1)+1)-(find(" ",B3,find(" ",B3,1)+1)-(find(" ",B3,1))+find(" ",B3,1))-1&" "&
    LEN(B3)-(find(" ",B3,1)+find(" ",B3,find(" ",B3,1)+1)-(find(" ",B3,1))+find(" ",B3,find(" ",B3,find(" ",B3,1)+1)+1)-(find(" ",B3,find(" ",B3,1)+1)-(find(" ",B3,1))+find(" ",B3,1)))
    

    【讨论】:

    • 哇,我真的很感谢你的努力 xD。
    【解决方案5】:

    也许有一个循环? 假设单词用空格隔开:

    Sub CountChars
        Dim strWord as Variant, lenWord as Long, StrCount as String
    
        For Each strWord in Split(mysheet.range("A1")," ")
            lenWord = len(strWord)
    
            'if you wish to display side by side:
            StrCount = StrCount & cstr(lenword) & " "
        Next
        StrCount = Trim(StrCount) 'To remove the space at the end from the last loop
        Debug.print StrCount
    
    End Sub
    

    【讨论】:

      【解决方案6】:

      您可以使用 Characters.Count 属性。

      我从here 得到以下脚本。

      Sub MakeSuperscript() 
       Dim n As Integer 
      
       n = Worksheets("Sheet1").Range("A1").Characters.Count 
       Worksheets("Sheet1").Range("A1").Characters(n, 1) _ 
       .Font.Superscript = True 
      End Sub
      

      因此,在您的情况下,我相信如果您要使用 .split() 函数来创建单词数组。然后在遍历数组的每个元素时,您可以使用Characters.count() 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        • 2021-06-18
        • 2018-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        相关资源
        最近更新 更多