How to add words to this function?

In this function, how would I use the lcount in a sentence such as:
There are 5 words in the sentence.

Function CountWords(rRange As Range) As Long
Dim rCell As Range, lCount As Long
Dim Sentence As String
    For Each rCell In rRange
        lCount = lCount + _
          Len(Trim(rCell)) - Len(Replace(Trim(rCell), " ", "")) + 1
    Next rCell
CountWords = lCount
End Function
LVL 1
Alex CampbellAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Msgbox "There are " & countwords(<blah>) & " words in the sentence."
0
Alex CampbellAuthor Commented:
Thanks, but how exactly should it go in the code?
0
Martin LissOlder than dirtCommented:
The following should display "There are 2 words in the sentence."
Sub Test()

Const ASENTENCE = "Hello World"
Msgbox "There are " & countwords(ASENTENCE) & " words in the sentence."
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Tom FarrrelCommented:
There is a variable Sentence that you do not use. You can use it to capture
Sentence = Trim(rCell)
and then use it in
lCount = lCount + Len(Sentence) - Len(Replace(Sentence, " ", "")) + 1

That makes your code easier to read and (not significantly) faster
0
Alex CampbellAuthor Commented:
What would the new code look like?

Function CountWords(rRange As Range) As Long
Dim rCell As Range, lCount As Long
Dim Sentence As String
    For Each rCell In rRange
        lCount = lCount + _
          Len(Trim(rCell)) - Len(Replace(Trim(rCell), " ", "")) + 1
    Next rCell
CountWords = lCount
End Function
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
i guess you meant to make the function returns as String instead of Long, like this?

Function CountWords(rRange As Range) As String
    Dim rCell As Range, lCount As Long
    Dim Sentence As String
    
    For Each rCell In rRange
        lCount = lCount + _
          Len(Trim(rCell)) - Len(Replace(Trim(rCell), " ", "")) + 1
    Next rCell
    CountWords = "There are " & lCount & " words in the sentence"
End Function

Open in new window

0
Alex CampbellAuthor Commented:
Thanks
0
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.