View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rich Locus Rich Locus is offline
external usenet poster
 
Posts: 74
Default Using Trim in a Userform

Hello:
If this function solves your problem,
please check the "Answers My Question" button.

This function trims leading and trailing spaces,
and then allows only one single space between
contiguous words in a string:

Function StripExtraSpaces(ByVal InputString As String) As String
Dim i, j As Long
Dim boolLastItemWasSpace As Boolean
j = 0
boolLastItemWasSpace = False

InputString = Trim(InputString)
For i = 1 To Len(InputString)
If Mid(InputString, i, 1&) = " " Then
If Not boolLastItemWasSpace Then
j = j + 1&
Mid(InputString, j, 1&) = Mid(InputString, i, 1&)
boolLastItemWasSpace = True
Else
boolLastItemWasSpace = False
End If
Else
j = j + 1&
Mid(InputString, j, 1&) = Mid(InputString, i, 1&)
End If
Next i
StripExtraSpaces = Left(InputString, j) ' String Is Now Shorter
End Function

--
Rich Locus
Logicwurks, LLC


"Curtd" wrote:

I have a Userform that I fill out and have the information entered in a cell
on a worksheet, I am trying to use Trim when entering it in the cell to
remove any duplicate spaces. Can someone help me out and let me know why
this isn't working and what I need to change to get it to work. Thank you
for your help.

ws.Cells(iRow, 2) = Trim(txtProblem.Value)