+ Reply to Thread
Results 1 to 3 of 3

Remove spaces and replace with comma.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Remove spaces and replace with comma.

    HI I have a csv file which seems a little cracy, and i would like a way to be able to work with it.
    I need to remove the spaces and and properly also the special caracters and replace them with a comma.
    And then save the csv file again. I have tried to record find replace, but it put allot of commas inside, i only need one between.
    I found below code, which maybe can be usefull. it remove all spaces, so i need that one to be maybe able to put a comma between.

    Please have a look
    Put a test sheet here and some lines of the csv file is in sheet 2

    Sincerely Abjac

    Here is the code which remove all spaces.

    Sub TrimBText()
    ' This module will trim extra spaces from BOTH SIDES.
        Dim MyCell As Range
        On Error Resume Next
            Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
            For Each MyCell In Selection.Cells
                MyCell.Value = Trim(MyCell.Value)
            Next
        On Error GoTo 0
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Remove spaces and replace with comma.

    Hi,

    is this what you need?
    Sub TrimBText()
        'Trim and replace spaces with commas, treat multiple spaces as 1
        Dim xlCell As Range
        For Each xlCell In Sheets(2).Cells.SpecialCells(xlCellTypeConstants)
            xlCell.Value = Trim(xlCell.Value)
            Do While InStr(xlCell.Value, "  ") > 0
                xlCell.Value = VBA.Replace(xlCell.Value, "  ", " ")
            Loop
            xlCell.Value = VBA.Replace(xlCell.Value, " ", ",")
        Next xlCell
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Remove spaces and replace with comma.

    Hi TehneXus. This was exactly what i was loooking for. Its really perfect thanks

    Have a nice evening

    Sincerely

    Abjac

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1