Consulting

Results 1 to 6 of 6

Thread: Solved: How Do I clear leading spaces from a column?

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    37
    Location

    Solved: How Do I clear leading spaces from a column?

    Hi, I have a column with 10,000 entries. I would to put some VBA code into a macro to delete spaces that occur after the text in the cells.

    I tried TRIM but as the name is diffferant in almost every cell it seeemed like a lot of work to code each one...

    thanks all!!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    could you post a couple of the entries and what you would like for them to look like after?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Jun 2007
    Posts
    37
    Location
    Column B has about 10,000 entries as below:
    "John Smith " - about 8 blank spaces after each name
    "Jane Match "
    "Steven Lutger "

    after I would like:
    "John Smith"
    "Jane Match"
    "Steven Lutger"

    thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Dim i As Long

    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    Cells(i, "A").Value = Trim$(Cells(i, "A").Value)
    Next i
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi alu,
    give this a try:
    [VBA]Sub TrimXcessSpaces()
    'Macro Purpose: To trim all excess spaces out of cells. This
    'eliminates issues where users have cleared the cell with a space,
    'and elimates all extra spaces at the beginning or end of a string
    Dim cl As Variant
    'Loop through cells removing excess spaces
    For Each cl In Selection
    If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then
    cl.Value = WorksheetFunction.Trim(cl)
    End If
    Next cl
    End Sub[/VBA]
    You will have to select the area you wish to run it on
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Regular
    Joined
    Jun 2007
    Posts
    37
    Location
    It Worked thanks a lot!!!

Posting Permissions

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