目前我的代码可以在TextBox中完美地计算单词字符,但是我如何从Textboxes开始计算WORDS。我只是想不通。下面我添加了需要修改的代码:
Sub CountCharFromTextBox()
Dim shp As Shape
Dim wks As Worksheet
Dim lTxtBoxWords As Long
For Each wks In ActiveWorkbook.Worksheets
For Each shp In wks.Shapes
If TypeName(shp) <> "GroupObject" Then
lTxtBoxWords = shp.TextFrame.Characters.Count
End If
Next shp
Next wks
MsgBox lTxtBoxWords
End Sub
我无法找到TextFrame的任何类似属性。 TextFrame2都不起作用。我不知道现在要做什么。
希望任何人都能找到改变的地方。
答案 0 :(得分:5)
Function countWords(ByVal sentence As String) As Integer
countWords = UBound(Split(sentence, " ")) + 1
End Function
说明:
Split()函数返回在指定的分隔符上拆分的字符串数组。例如,split(“Carl is awesome”,“”)将拆分为“”(空格)并返回: [“卡尔”,“是”,“太棒了”]。该数组的索引是0-2。
Ubound()返回数组中最后一个元素的索引。由于split()的数组从0开始,我们需要在ubound()的结果中加1。
函数CountWords()接受一个字符串并返回空格数+ 1,这几乎可以肯定是单词的数量。您可以考虑检查split()返回的元素的长度,以捕获0个长度的“单词”,即双倍空格或前导或尾随空格。
答案 1 :(得分:1)
你去吧
Sub CountCharFromTextBoxV2()
For Each shp In ActiveSheet.Shapes
ActiveSheet.Shapes.Range(Array(shp.Name)).Select
theString = Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text
theNumWords = Len(Trim(theString)) - Len(Replace(Trim(theString), " ", "")) + 1
MsgBox "TextBox Name: " & shp.Name & vbNewLine & vbNewLine & "Number of words: " & theNumWords
Next
End Sub
答案 2 :(得分:1)
感谢大卫。他给了我正确的灵感。终于找到了代码。谢谢我和大卫。现在我也可以与他人分享:
Sub CountWordsFromTextBox()
Dim shp As Shape
Dim wks As Worksheet
Dim lTxtBoxWords As String
theNumWords = 0
For Each wks In ActiveWorkbook.Worksheets
For Each shp In wks.Shapes
If TypeName(shp) <> "GroupObject" And shp.TextFrame2.TextRange.Characters.Text <> "" Then
lTxtBoxWords = shp.TextFrame2.TextRange.Characters.Text
theNumWords = theNumWords + Len(Trim(lTxtBoxWords)) - Len(Replace(Trim(lTxtBoxWords), " ", "")) + 1
End If
Next shp
Next wks
MsgBox theNumWords
End Sub
答案 3 :(得分:0)
如果单词是由空格分隔的字符串,那么您可以计算任何字符串中的单词,如:
Sub WordCount()
Dim s As String
s = "klaatu barada nikto"
With Application.WorksheetFunction
MsgBox UBound(Split(.Trim(s), " ")) + 1
End With
End Sub
这里 Trim()用于删除任何无关的空格
修改#1:强>
以下是将其应用于TextBox的方法。首先创建TextBox:
Sub BoxMaker()
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 217.5, 51#, _
482.25, 278.25).Select
Selection.Name = "SPLASH"
Selection.Characters.Text = "Please Wait for Macro"
With Selection.Characters(Start:=1, Length:=21).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 36
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
End Sub
这就是我如何计算TextBox中的单词:
Sub WordCounter2()
Dim s As String
ActiveSheet.Shapes("SPLASH").Select
s = Selection.Characters.Text
With Application.WorksheetFunction
MsgBox UBound(Split(.Trim(s), " ")) + 1
End With
End Sub
答案 4 :(得分:0)
尝试使用以下代码
<强>测试强>
Sub CountCharFromTextBox()
Dim shp As Shape
Dim wks As Worksheet
Dim lTxtBoxWords As Long
Dim lTxtBoxWordsnew As Long
For Each wks In ActiveWorkbook.Worksheets
For Each shp In wks.Shapes
If TypeName(shp) <> "GroupObject" Then
lTxtBoxWords = shp.TextFrame.Characters.Count
lTxtBoxWordsnew = getwordscount(shp.TextFrame.Characters.text)
End If
Next shp
Next wks
MsgBox lTxtBoxWordsnew
End Sub
Private Function getwordscount(text As String)
getwordscount = Len(text) - Len(Application.WorksheetFunction.Substitute(text, " ", "")) + 1
End Function
答案 5 :(得分:0)
很棒的代码!在PowerPoint中可以使用它吗?
Sub CountWordsFromTextBox()
Dim shp As Shape
Dim wks As Worksheet
Dim lTxtBoxWords As String
theNumWords = 0
For Each wks In ActiveWorkbook.Worksheets
For Each shp In wks.Shapes
If TypeName(shp) <> "GroupObject" And shp.TextFrame2.TextRange.Characters.Text <> "" Then
lTxtBoxWords = shp.TextFrame2.TextRange.Characters.Text
theNumWords = theNumWords + Len(Trim(lTxtBoxWords)) - Len(Replace(Trim(lTxtBoxWords), " ", "")) + 1
End If
Next shp
Next wks
MsgBox theNumWords
End Sub