There are a few ways you can turn a three-word phrase into the first two words.
Let's start with your Split()
method.
This function returns an array. Your particular method of attempting to access the index will only return a single word.
You can place into an array, then just combine the array elements:
For Each r In Range(...)
Dim retVal() As String
retVal = Split(r.Value)
r.Value = retVal(0) & " " & retVal(1)
Next r
You can remove the last word with Replace()
:
For Each r In Range(...)
r.Value = Replace(r.Value, Split(r.Value)(2), "")
Next
Or you can even use Regular Expressions:
With CreateObject("VBScript.RegExp")
.Pattern = "\s[^\s]+$"
For Each r in Range(...)
r.Value = .Replace(r.Value, "")
Next
End With
In Regular Expressions, \s
signifies a single space character, the [^...]
bracket means "Do not include", which we placed a \s
within the bracket, so that would match any non-space character, followed by the +
means 1 or more times, and finally the $
signifies the end of the string. Essentially, you are wanting to match a word [^\s]+
that is at the end of the string $
, preceeded by a space \s
, and remove it via the .Replace()
method. And you actually could also simply use the pattern \s\S+$
, which is essentially the same thing (\S
means any non-space character when it's capitalized).