Results 1 to 6 of 6

Thread: Macro to remove extra spaces

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Location
    Sunny Surrey (near a small town called London).
    Posts
    38

    Question Macro to remove extra spaces

    Hi, I am getting data in which has multiple spaces in, I have tried using the following code which works in both VB5 and VB6, but does not appear to in VBA. Can anyone point me as how to amend this. Thanks


    Public Function RemoveExtraSpaces(str As String) As String
    ' Removes Extra Spaces in between the words

    str = Trim(str)

    Dim L As Integer
    Dim i As Integer
    Dim S As String
    Dim Prev_char As String * 1

    S = ""

    L = Len(str)
    i = 1
    Do
    Prev_char = Mid(str, i, 1)
    i = i + 1

    S = S + Prev_char
    If Prev_char = " " Then
    Do While (i < L) And (Mid(str, i, 1) = " ")
    i = i + 1
    Loop
    End If

    Loop Until i > L
    str = S
    RemoveExtraSpaces = S

    End Function

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Macro to remove extra spaces

    i do not see any reason why it should not work as well in vba as in vb6
    but could be simplified a bit
    tested in vba
    vb Code:
    1. mystr = "          asd       ddgfdf        eeoodldl      "
    2. mystr = Trim(mystr)
    3. s = ""
    4. For i = 1 To Len(mystr) - 1
    5.     If Mid(mystr, i, 1) = " " And Mid(mystr, i + 1, 1) = " " Then
    6.     Else: s = s & Mid(mystr, i, 1)
    7.     End If
    8. Next
    9. s = s & Mid(mystr, i, 1)
    10. mystr = s
    in vb6, office 2000 or later you could use
    mystr = Trim(Replace(Replace(mystr, " ", " "), " ", " "))

    you should use & to join strings
    avoid using reserved words (Str) for variables as that may make a difference in vba
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Macro to remove extra spaces

    pete, people can't see double spaces in this:
    mystr = Trim(Replace(Replace(mystr, " ", " "), " ", " "))

    If Replace() is available then use this:
    Code:
    str1 = Trim(str1)
    Do While Instr(str1, "  ")
       str1 = Replace(str1, "  ", " ")
    Loop
    If you are working with Excel VBA then you can use this:
    Code:
    str1 = Application.WorksheetFunction.Trim(str1)
    You will see the different between VBA.Trim() function and WorksheetFunction.Trim().
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2010
    Location
    Sunny Surrey (near a small town called London).
    Posts
    38

    Re: Macro to remove extra spaces

    Hi Guys, thanks for your suggestions. I think however there is an issue relating to finding and replacing spaces compared with characters and symbols.

    With other data I get, I also have an issue with random extra 7 zeros occasionally in it. So 123400000005678 rather than the valid data of 12345678. I tried "temp = Replace(String(7, "0"), "", , 1)" and suprisingly that didn't work either, was going to try that for the spaces next.

    I have also tried "temp1 = Replace((temp1), " ", " ") 'replace 4 empty spaces" for both the spaces and zeros with no success. Initially looking at some form of VBLookup command, but guess what? Wasn't playing either.

    Going round in circles definately bugging me now.......

    Anhn excuse my ignorance (self taught) but what exactly do you mean by str1 = Application.WorksheetFunction.Trim(str1), why would I want to do that if reading the data from a string rather than a worksheet?

    Thanks

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Macro to remove extra spaces

    excels worksheet function trim removes all excess spaces from any string, prefix suffix and more than 1 between words
    but is irrelevant for any other character

    anhn's do loop will work to replace any number of any consequative characters, to a single character

    note replace is not available in vb5 or vba in office 97

    temp = Replace(String(7, "0"), "", , 1)
    this would work if you got it right
    vb Code:
    1. temp = "123400000005678"
    2. temp = Replace(temp, String(7, "0"), "")
    3. MsgBox temp
    i tested to make sure vba excel 2000
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Location
    Sunny Surrey (near a small town called London).
    Posts
    38

    Re: Macro to remove extra spaces

    Quote Originally Posted by westconn1 View Post
    excels worksheet function trim removes all excess spaces from any string, prefix suffix and more than 1 between words
    but is irrelevant for any other character

    I was under the belief that trim only removed spaces before and after words/sentances, so basically adding Ltrim and Rtrim together.

    anhn's do loop will work to replace any number of any consequative characters, to a single character

    I have tried anhn's loop trying to replace characters, I tried m & m, so 'mummy' to 'mumy', which has not appeared to work. I need to go back and look at my general data reading and processing and coding of it.

    note replace is not available in vb5 or vba in office 97

    I have office 2002 with vb6.5, replace works.

    temp = Replace(String(7, "0"), "", , 1)
    this would work if you got it right
    vb Code:
    1. temp = "123400000005678"
    2. temp = Replace(temp, String(7, "0"), "")
    3. MsgBox temp
    i tested to make sure vba excel 2000
    I will go try that, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width