From: Rick Rothstein on 24 May 2010 00:14 When I run your code, I get two spaces left between words, not one. I'm guessing you didn't see my first posting in this thread, did you? Here is what I posted modified to fit your function header... Function StripExtraSpaces(ByVal InputString As String) As String StripExtraSpaces = WorksheetFunction.Trim(InputString) End Function This code, which is much shorter than yours, outputs what I think you intended your function to output. -- Rick (MVP - Excel) "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message news:EA5C74DA-22EE-4B4C-BE44-8DEDF3230804(a)microsoft.com... > 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)
From: Rich Locus on 24 May 2010 01:08 Rick: You are correct. I like your approach better. I am, however, still going to fix the two space bug in the function. Regards, -- Rich Locus Logicwurks, LLC "Rick Rothstein" wrote: > When I run your code, I get two spaces left between words, not one. > > I'm guessing you didn't see my first posting in this thread, did you? Here > is what I posted modified to fit your function header... > > Function StripExtraSpaces(ByVal InputString As String) As String > StripExtraSpaces = WorksheetFunction.Trim(InputString) > End Function > > This code, which is much shorter than yours, outputs what I think you > intended your function to output. > > -- > Rick (MVP - Excel) > > > > "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message > news:EA5C74DA-22EE-4B4C-BE44-8DEDF3230804(a)microsoft.com... > > 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) > > . >
From: Rich Locus on 24 May 2010 01:16 Rick: Here is the corrected code which can be used to strip any character... not just spaces, by changing the literal. However, for spaces, you definitely have the correct solutions. Regards to the MVP. 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 End If Else j = j + 1& Mid(InputString, j, 1&) = Mid(InputString, i, 1&) boolLastItemWasSpace = False End If Next i StripExtraSpaces = Left(InputString, j) ' String Is Now Shorter End Function -- Rich Locus Logicwurks, LLC "Rich Locus" wrote: > Rick: > You are correct. I like your approach better. I am, however, still going > to fix the two space bug in the function. > Regards, > -- > Rich Locus > Logicwurks, LLC > > > "Rick Rothstein" wrote: > > > When I run your code, I get two spaces left between words, not one. > > > > I'm guessing you didn't see my first posting in this thread, did you? Here > > is what I posted modified to fit your function header... > > > > Function StripExtraSpaces(ByVal InputString As String) As String > > StripExtraSpaces = WorksheetFunction.Trim(InputString) > > End Function > > > > This code, which is much shorter than yours, outputs what I think you > > intended your function to output. > > > > -- > > Rick (MVP - Excel) > > > > > > > > "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message > > news:EA5C74DA-22EE-4B4C-BE44-8DEDF3230804(a)microsoft.com... > > > 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) > > > > . > >
From: Rick Rothstein on 24 May 2010 01:19 Well, if you want to do this without calling the WorksheetFunction property (of the Application object), then I would use this (still) much shorter macro... Function StripExtraSpaces(ByVal InputString As String) As String StripExtraSpaces = Trim(InputString) Do While InStr(StripExtraSpaces, " ") StripExtraSpaces = Replace(StripExtraSpaces, " ", " ") Loop End Function -- Rick (MVP - Excel) "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message news:30E75A12-6D9C-4ECD-B508-54F7BA94E7C6(a)microsoft.com... > Rick: > You are correct. I like your approach better. I am, however, still going > to fix the two space bug in the function. > Regards, > -- > Rich Locus > Logicwurks, LLC > > > "Rick Rothstein" wrote: > >> When I run your code, I get two spaces left between words, not one. >> >> I'm guessing you didn't see my first posting in this thread, did you? >> Here >> is what I posted modified to fit your function header... >> >> Function StripExtraSpaces(ByVal InputString As String) As String >> StripExtraSpaces = WorksheetFunction.Trim(InputString) >> End Function >> >> This code, which is much shorter than yours, outputs what I think you >> intended your function to output. >> >> -- >> Rick (MVP - Excel) >> >> >> >> "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message >> news:EA5C74DA-22EE-4B4C-BE44-8DEDF3230804(a)microsoft.com... >> > 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) >> >> . >>
From: Rick Rothstein on 24 May 2010 01:32 I'm not sure how much sense it makes to remove non-space characters from the beginning and end of a text string and then collapse multiple internal non-space characters down to a single occurrence of that non-space character, but if that is what you want, then I would probably do it this way (where you can pass in the non-space character in the optional 2nd argument, which defaults to the space character if omitted)... Function StripExtraSpaces(ByVal InputString As String, _ Optional ReplaceChar As String = " ") As String If ReplaceChar <> " " Then InputString = Replace(InputString, " ", Chr(1)) InputString = Replace(InputString, ReplaceChar, " ") End If InputString = Trim(InputString) Do While InStr(InputString, " ") InputString = Replace(InputString, " ", " ") Loop If ReplaceChar <> " " Then InputString = Replace(InputString, " ", ReplaceChar) InputString = Replace(InputString, Chr(1), " ") End If StripExtraSpaces = InputString End Function -- Rick (MVP - Excel) "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message news:C897DDEC-6B60-46D8-8362-8801941F37C0(a)microsoft.com... > Rick: > Here is the corrected code which can be used to strip any character... not > just spaces, by changing the literal. However, for spaces, you definitely > have the correct solutions. > > Regards to the MVP. > > 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 > End If > Else > j = j + 1& > Mid(InputString, j, 1&) = Mid(InputString, i, 1&) > boolLastItemWasSpace = False > End If > Next i > StripExtraSpaces = Left(InputString, j) ' String Is Now Shorter > End Function > > -- > Rich Locus > Logicwurks, LLC > > > "Rich Locus" wrote: > >> Rick: >> You are correct. I like your approach better. I am, however, still >> going >> to fix the two space bug in the function. >> Regards, >> -- >> Rich Locus >> Logicwurks, LLC >> >> >> "Rick Rothstein" wrote: >> >> > When I run your code, I get two spaces left between words, not one. >> > >> > I'm guessing you didn't see my first posting in this thread, did you? >> > Here >> > is what I posted modified to fit your function header... >> > >> > Function StripExtraSpaces(ByVal InputString As String) As String >> > StripExtraSpaces = WorksheetFunction.Trim(InputString) >> > End Function >> > >> > This code, which is much shorter than yours, outputs what I think you >> > intended your function to output. >> > >> > -- >> > Rick (MVP - Excel) >> > >> > >> > >> > "Rich Locus" <RichLocus(a)discussions.microsoft.com> wrote in message >> > news:EA5C74DA-22EE-4B4C-BE44-8DEDF3230804(a)microsoft.com... >> > > 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) >> > >> > . >> >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Date programing in a cell Next: please help.. highly important... |