
[英]The average number of characters per word in each paragraph? in an excel cell
[英]Count number of characters of each word in a cell in excel
例如,如果我在一个单元格中有一句话:
The fox jumped twice
输出应该是
3 3 6 5
我已经尝试在几个组合中使用len()
和trim()
函数,但还没有设法找到解决方案,担心只能通过使用 VBA 来完成。
也许有一个循环? 假设单词用空格分隔:
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
User-Defined-Function 是一个好主意,确实:
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()
删除最后一个空格;只是为了展示你在没有 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)))
您可以使用Characters.Count属性。
我从这里得到了以下脚本。
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()
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.