Dim strTMP as String
Dim Lines() as string
Dim count as Long
strTMP = Replace(txtInput, vbNewLine, " ")
Lines = Split(strTMP, " ")
count = 0
For i = LBound(Lines) To UBound(Lines)
If Len(Lines(i)) > 0 Then count = count + 1
Next
MsgBox "Number of Words: " & Str(count), vbInformation, "Result"
End Sub
We get the text in a Variable and then replace newlines with spaces, because we will count the words from the same line. next we split the text whenever we find a space " " and store the array into the Lines variable. Then we run a loop from the lower bound of Lines to its upper bound and check to see if the current item of the array is not empty, if its not empty we increase the count of words by 1. This process is repeated, thanks to the loop and at last we display the count of words in a message box.