Dim myStr As String
Dim Result As String
myStr = [A2]
Result = Left(myStr, InStr(InStr(1, myStr, " ") + 1, myStr, " ", vbTextCompare)-1)
Function First2Words(myStr As Variant) As Variant
First2Words = Left(myStr, InStr(InStr(1, myStr, " ") + 1, myStr, " ", vbTextCompare) - 1)
End Function
Dim strInputString as string
Dim vData As Variant
vData = Split(strInputString & " ", " ")
MsgBox Trim(vData(0) & " " & vData(1))
@ Hui,Will your string always have two spaces?
Code:Dim strInputString as string Dim vData As Variant vData = Split(strInputString & " ", " ") MsgBox Trim(vData(0) & " " & vData(1))
Hello Hui,Code:Dim myStr As String Dim Result As String myStr = [A2] Result = Left(myStr, InStr(InStr(1, myStr, " ") + 1, myStr, " ", vbTextCompare)-1)
or as a VBA Function
Code:Function First2Words(myStr As Variant) As Variant First2Words = Left(myStr, InStr(InStr(1, myStr, " ") + 1, myStr, " ", vbTextCompare) - 1) End Function
as a function in Excel use =First2Words(A2)