VBA Syntax Help Needed to Test Cells for More Than 1 Word (String)

beartooth91

New Member
Joined
Dec 15, 2024
Messages
38
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello All -

Looking for some help - with the syntax - for testing each cell - in a column - for more than one word (string) entry in each cell. Not looking for specific words, just more than one word in the cell. (A valid entry is just one word/string.) Invalid entries - more than one word/string - are usually separated by a comma and space or just a space between the two. Assuming I'd use an If-Then-Else loop, possibly with Select Case. The main problem is I don't know the search string syntax to look for two strings separated by a space or two strings separated by a comma and space.....?

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
If you find a cell with more than one word, do you just need to flag it as invalid? Or extract the words?

The simple way to detect this is:
VBA Code:
If InStr(Cell, " ") > 0 Then
where Cell is a Range reference to a cell

You seem to know how to wrap a loop around this but if you need help with that part let me know. I need to know which column, and what rows the cells are in.
 
Upvote 0
Solution
Here is a little User Defined Function I created that you can call from your VBA code (or straight from the function on your sheet):
VBA Code:
Function CountWords(str As String) As Long

    Dim c1 As Long
    Dim c2 As Long
    Dim arr1() As String
    Dim arr2() As String
   
'   Split string first on spaces
    arr1 = Split(Trim(str), " ")
'   Get count of words
    c1 = UBound(arr1) + 1
   
'   Split string first on commas
    arr2 = Split(Trim(str), ",")
'   Get count of words
    c2 = UBound(arr2) + 1
   
'   Return larger of two counts
    If c1 > c2 Then
        CountWords = c1
    Else
        CountWords = c2
    End If
   
End Function

Here is an example of it working right on the worksheet:
1735575991303.png


Here is the formula in cell B1, copied down:
Excel Formula:
=CountWords(A1)

This will count and return the number of words, regardless of whether they separated by spaces, commas, and a combination of both.
 
Upvote 0
You didn't say what you wanted to do with them, but this will put a Yes or No in Column B

VBA Code:
Sub TestForMultipleWords()
Dim c As Range, s As String, t
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    s = Application.Trim(Replace(c.Text, ",", " "))
    If UBound(Split(s, " ")) > 0 Then c.Offset(, 1) = "Yes" Else c.Offset(, 1) = "No"
Next
End Sub

For a count of the words:
VBA Code:
Sub TestForMultipleWords()
Dim c As Range, s As String, t
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    s = Application.Trim(Replace(c.Text, ",", " "))
    c.Offset(, 1) = UBound(Split(s, " "))  + 1
Next
End Sub
 
Last edited:
Upvote 0
And here is some VBA code that calls my UDF and loops through the values in a range to return cells where there are more than one word:
VBA Code:
Sub Test()
    Dim cell As Range
    For Each cell In Range("A1:A10")
        If CountWords(cell.Value) > 1 Then
            MsgBox "More than one word in cell : " & cell.Address(0, 0)
        End If
    Next cell
End Sub
 
Upvote 0
Hello All -

Looking for some help - with the syntax - for testing each cell - in a column - for more than one word (string) entry in each cell. Not looking for specific words, just more than one word in the cell. (A valid entry is just one word/string.) Invalid entries - more than one word/string - are usually separated by a comma and space or just a space between the two. Assuming I'd use an If-Then-Else loop, possibly with Select Case. The main problem is I don't know the search string syntax to look for two strings separated by a space or two strings separated by a comma and space.....?

Thanks
I'm not sure what you want to do with the result.

No VBA solution.

All you have to do is replace the range reference $A$2:$A$9.

Expected Sub Function.xlsm
ABCDEF
1DataValid EntriesWordsInvalid EntriesWords
24e5#F9iuvs,zMWfCUTotxWizStRxDUE14e5#F9iuvs,zMWfCUTotx2
34UoeJ36P28,JEz*hhAMc%A!RNrVksn214UoeJ36P28,JEz*hhAMc%2
4X$bzQ0^7N9 V*OWQg3FPR00jQ1NexYw1X$bzQ0^7N9 V*OWQg3FPR2
5e!3YbBJjug, E&*^D1!CY8, 7L^B8X@ifH, 9TG#MaDD34e!3YbBJjug, E&*^D1!CY8, 7L^B8X@ifH, 9TG#MaDD344
6WizStRxDUEHJln0O6cxX, f5&2bUVTZF, %GuM@OTXMu, qN!4p&3r!G4
7A!RNrVksn2
8HJln0O6cxX, f5&2bUVTZF, %GuM@OTXMu, qN!4p&3r!G
900jQ1NexYw
Sheet3
Cell Formulas
RangeFormula
C2:D4C2=LET(o,TRIM($A$2:$A$9),d,TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(o,","," "),", "," ")," "," ")),e,SUBSTITUTE(d," ",""),c,(LEN(d)-LEN(e))+1,r,HSTACK(o,c),FILTER(r,INDEX(r,,2)=1))
E2:F6E2=LET(o,TRIM($A$2:$A$9),d,TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(o,","," "),", "," ")," "," ")),e,SUBSTITUTE(d," ",""),c,(LEN(d)-LEN(e))+1,r,HSTACK(o,c),FILTER(r,INDEX(r,,2)>1))
Dynamic array formulas.
 
Upvote 0
If you find a cell with more than one word, do you just need to flag it as invalid? Or extract the words?

The simple way to detect this is:
VBA Code:
If InStr(Cell, " ") > 0 Then
where Cell is a Range reference to a cell

You seem to know how to wrap a loop around this but if you need help with that part let me know. I need to know which column, and what rows the cells are in.
This worked....Thanks. Its part of a series of valid entry checks for a particular column. If the entries are not proper; I just highlight them for review and correction.

I ended up with:

VBA Code:
'Check for invalid ZEROSTATE and ONESTATE entries

   Entry = .Cells(i, "AS")

   If InStr(Entry, " ") > 0 Then

      .Cells(i, "AS").Interior.ColorIndex = 45

      g = g + 1

   End If

   Entry2 = .Cells(i, "AT")

   If InStr(Entry2, " ") > 0 Then

      .Cells(i, "AT").Interior.ColorIndex = 45

      g = g + 1

   End If
 
Upvote 0

Forum statistics

Threads
1,225,135
Messages
6,183,065
Members
453,147
Latest member
Lacey D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top