Trimming anything other than the first word

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this function:

Public Function FirstWord(InputString As String) As String
Dim FirstSpace As String
FirstSpace = InStr(1, InputString, " ")
FirstWord = Left(InputString, FirstSpace - 1)
End Function

This trims after the first apsce yet users are putting in spaces at the
start then this deletes the whole data.

Any suggestions on something more robust and a message that tells what data
will be deleted?
 
The Trim function (or maybe LTrim) should work. Try something like:
FirstSpace = InStr(1, Trim(InputString), " ")
VBA Help has more information on the Trim function.
 
I left out a reply to the second part of your question. You could do
something like:

Public Function FirstWord(InputString As String) As String
Dim FirstSpace As String, CutOut as String
FirstSpace = InStr(1, (Trim(InputString), " ")
FirstWord = Left(InputString, FirstSpace - 1)
CutOut = Right(Trim(InputString),Len(InputString) - Len(FirstWord))
MsgBox """ & CutOut & """ has been removed"
End Function
 
Back
Top