Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    St. Charles, Illinois
    Posts
    222
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove carriage returns and extra spaces (Excel 2000)

    I need to remove extra carriage returns and more than 1 extra space in a cell. I origianlly needed to do this in Access, but I could not get this code sent by Hans to work. Is there a macro that will remove the extra spaces and carriage returns? Here is the code:

    Function ReplaceChars(s) As Variant
    Dim lngLen As Long
    Dim strOld As String
    Dim strNew As String

    If IsNull(s) Then Exit Function

    strNew = Trim(s)
    Do
    strOld = strNew
    lngLen = Len(strOld)
    strNew = Replace(strOld, Space(2), Space(1))
    Loop Until Len(strNew) = lngLen
    Do
    strOld = strNew
    lngLen = Len(strOld)
    strNew = Replace(strOld, vbCrLf & Space(1), vbCrLf)
    Loop Until Len(strNew) = lngLen
    Do
    strOld = strNew
    lngLen = Len(strOld)
    strNew = Replace(strOld, Space(1) & vbCrLf, vbCrLf)
    Loop Until Len(strNew) = lngLen
    Do
    strOld = strNew
    lngLen = Len(strOld)
    strNew = Replace(strOld, vbCrLf & vbCrLf, vbCrLf)
    Loop Until Len(strNew) = lngLen
    ReplaceChars = Trim(strNew)
    End Function

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Remove carriage returns and extra spaces (Excel 2000)

    [sulk]

    In what sense couldn't you get the code to work in Access? Did you get error messages, or didn't it do anything, or did it yield incorrect results?

    [/sulk]

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    St. Charles, Illinois
    Posts
    222
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove carriage returns and extra spaces (Excel 2000)

    Oh please don't sulk. You are brilliant and so kind. The problem is me. I created the new module and now I can't get it to run and I don't have my Access books with me to assist. [img]/forums/images/smilies/sad.gif[/img]

  4. #4
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Remove carriage returns and extra spaces (Excel 2000)

    If you want to do it in excel (since that is where you posted) you can use a combination of the build in functions:

    <pre>=trim(Clean(A1))</pre>


    assuming A1 has the string to convert. Copy this down the rows to convert all the column. Then you can pastespecial value the contents if desired.

    Steve

  5. #5
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    St. Charles, Illinois
    Posts
    222
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Remove carriage returns and extra spaces (Excel 2000)

    This was beautiful! I used this function to eliminate extra spaces, etc. Thank you, thank you, thank you!

Posting Permissions

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