[英]Count number of characters of each word in a cell in excel
For instance if I have the sentence in one cell:例如,如果我在一个单元格中有一句话:
The fox jumped twice
the output should be输出应该是
3 3 6 5
I've tried using the len()
and trim()
functions in a couple of combinations but didn't manage to find a solution yet, was afraid that it could be done only by using VBA.我已经尝试在几个组合中使用
len()
和trim()
函数,但还没有设法找到解决方案,担心只能通过使用 VBA 来完成。
maybe with a loop?也许有一个循环? Presuming that the words are separated by spaces:
假设单词用空格分隔:
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
A User-Defined-Function is a good idea, indeed: 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()
does not need an argument what to split with, if it is space;Split()
不需要参数来拆分什么;Trim()
removes the last space; Trim()
删除最后一个空格;Just to show the horrible answer you get without VBA:只是为了展示你在没有 VBA 的情况下得到的可怕答案:
You can look for the " " and then count the length between the different spaces.您可以查找“”,然后计算不同空格之间的长度。
=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)))
You could use the Characters.Count property.您可以使用Characters.Count属性。
I got the following script from 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
So in your case I believe if you were to use the .split()
function to create an array of words.所以在你的情况下,我相信你是否要使用
.split()
函数来创建一个单词数组。 Then while looping through each element of the array you can use the Characters.count()
property.然后在循环遍历数组的每个元素时,您可以使用
Characters.count()
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.