在Excel VBA中从TextBox中统计单词

目前我有在TextBox中完美地计算单词字符的代码,但是我怎样才能从文本框开始统计单词。 我只是不知道。 下面我添加了需要修改的代码:

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都不起作用。 我现在不知道该怎么做。

希望有人会find改变。

 Function countWords(ByVal sentence As String) As Integer countWords = UBound(Split(sentence, " ")) + 1 End Function 

说明:

Split()函数返回一个在指定的分隔符处分割的string数组。 例如,split(“Carl is awesome”,“”)将在“”(空格)上分割并返回:[“Carl”,“is”,“awesome”]。 这个数组的索引是0-2。

Ubound()返回数组中最后一个元素的索引。 由于split()中的数组从0开始,所以我们需要在ubound()的结果中加1。

函数CountWords()接受一个string并返回空格+ 1的数量,这几乎肯定是单词的数量。 您可能会考虑检查split()返回的元素的长度,以捕获0长度的“单词”,即双空格或前导或尾随空格。

干得好

 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 

感谢大卫。 他给了我正确的灵感。 代码终于find了。 感谢我和David。 现在我也可以与其他人分享:

  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 

如果单词是由空格分隔的string,那么您可以计算任何string中的单词,如:

 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:

这是我将如何应用到一个文本框。 首先创build文本框:

 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 

这里是我将如何计算该文本框中的单词:

 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 

尝试使用下面的代码

经testing

 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 

在这里输入图像说明

Interesting Posts