简体   繁体   English

从 Excel VBA 中的 TextBox 计算单词

[英]Count Words from TextBox in Excel VBA

I have code that counts characters in a 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

How can I count words from Textboxes?如何从文本框中计算单词?

I can't find any similar Property for TextFrame.我找不到任何类似的 TextFrame 属性。 TextFrame2 doesnt work. TextFrame2 不起作用。

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

Explanation:说明:

The Split() function returns an array of strings split on a delimiter you specify. Split() 函数返回在您指定的分隔符上拆分的字符串数组。 For example, split("Carl is awesome"," ") would split on " " (a space) and return: ["Carl", "is", "awesome"].例如, split("Carl is awesome"," ") 将拆分为 " "(一个空格)并返回:["Carl", "is", "awesome"]。 The indices of this array are 0-2.该数组的索引为 0-2。

Ubound() returns the index of the last element in an array. Ubound() 返回数组中最后一个元素的索引。 Since arrays from split() start at 0, we need to add 1 to the result of ubound().由于 split() 中的数组从 0 开始,我们需要将 1 添加到 ubound() 的结果中。

The function CountWords() takes a string and returns the number of spaces+1, which is almost certainly the number of words.函数 CountWords() 接受一个字符串并返回空格数+1,这几乎可以肯定是单词数。 You might consider checking the length of elements returned by split() to catch 0-length "words", ie double spaces or leading or trailing spaces.您可能会考虑检查 split() 返回的元素的长度以捕获长度为 0 的“单词”,即双空格或前导或尾随空格。

Here you go给你

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

Thanks to David.感谢大卫。 He gave me the right inspiration.他给了我正确的灵感。 The code is finally found.代码终于找到了。 Thanks for me and David.谢谢我和大卫。 Now I can share with others too:现在我也可以与他人分享:

    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

If words are character strings separated by a space, then you can count words in any string like:如果单词是由空格分隔的字符串,那么您可以计算任何字符串中的单词,例如:

Sub WordCount()
    Dim s As String

    s = "klaatu barada nikto"
    With Application.WorksheetFunction
        MsgBox UBound(Split(.Trim(s), " ")) + 1
    End With
End Sub

Here Trim() is used to remove any extraneous spaces这里Trim()用于删除任何多余的空格

EDIT#1:编辑#1:

Here is how I would apply this to a TextBox.这是我将它应用于 TextBox 的方法。 First create the 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

and here is how I would count the words in that 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

Try with below code尝试使用以下代码

Tested已测试

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

在此处输入图片说明

Great code!很棒的代码! Is it possible to use this in PowerPoint?是否可以在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM