Private Sub CommandButton1_Click()
Dim MyStr As String
MyStr = Replace(TextBoxComments.Text, Chr(10), " ")
MyStr = Replace(MyStr, Chr(13), " ")
Dim last_spc As Integer, first_spc As Integer
last_spc = InStr(TextBoxComments.SelStart, MyStr, " ")
lftpiece = Left(MyStr, last_spc)
first_spc = InStrRev(lftpiece, " ", Len(lftpiece) - 1) + 1
a = MsgBox(Mid(lftpiece, first_spc, Len(lftpiece) - first_spc), vbOKOnly)
End Sub
add the following to your userform
Code:Private Sub CommandButton1_Click() Dim MyStr As String MyStr = Replace(TextBoxComments.Text, Chr(10), " ") MyStr = Replace(MyStr, Chr(13), " ") Dim last_spc As Integer, first_spc As Integer last_spc = InStr(TextBoxComments.SelStart, MyStr, " ") lftpiece = Left(MyStr, last_spc) first_spc = InStrRev(lftpiece, " ", Len(lftpiece) - 1) + 1 a = MsgBox(Mid(lftpiece, first_spc, Len(lftpiece) - first_spc), vbOKOnly) End Sub
enjoy
Private Sub CommandButton1_Click()
Dim ans As String
Dim MyStr As String
MyStr = Replace(TextBoxComments.Text, Chr(10), " ")
MyStr = Replace(MyStr, Chr(13), "")
Dim s As Variant
s = Split(MyStr, " ")
Dim comstr As String
comstr = ""
Dim i As Integer
For i = 0 To UBound(s)
comstr = comstr + s(i)
If Len(comstr) > TextBoxComments.SelStart Then
ans = s(i)
Exit For
End If
comstr = comstr + " "
Next i
a = MsgBox(ans)
End Sub
I never liked the original code but I was in a hurry
Try the following:
Code:Private Sub CommandButton1_Click() Dim ans As String Dim MyStr As String MyStr = Replace(TextBoxComments.Text, Chr(10), " ") MyStr = Replace(MyStr, Chr(13), "") Dim s As Variant s = Split(MyStr, " ") Dim comstr As String comstr = "" Dim i As Integer For i = 0 To UBound(s) comstr = comstr + s(i) If Len(comstr) > TextBoxComments.SelStart Then ans = s(i) Exit For End If comstr = comstr + " " Next i a = MsgBox(ans) End Sub