例如,如果我在一个单元格中包含句子:
VerticalAlignment
输出应为
The fox jumped twice
我尝试将3 3 6 5
和len()
函数结合使用,但还没有找到解决方案,担心只能通过使用VBA来完成。 / p>
答案 0 :(得分:2)
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
答案 1 :(得分:1)
也许有循环? 假设单词之间用空格隔开:
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
答案 2 :(得分:1)
尝试这种基于短数组的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
答案 3 :(得分:1)
用户定义功能确实是个好主意:
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 :(得分:1)
仅显示您在没有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)))
答案 5 :(得分:0)
您可以使用 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()
属性。