+ Reply to Thread
Results 1 to 4 of 4

trim

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    52

    trim

    Hi,

    I need to trim additional spaces from cells within the below range

            Range("A140").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
    I'm not sure how/where to include it?

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: trim

    How about this?

    For Each rCell In Selection
    
    rCell.Value = Trim(rCell.Value)
    
    Next
    You should dimension the rCell variable at the top of your code

    Dim rCell as range
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: trim

    It's not clear how the range is populated - presumably constants ?

    If so you could get away with a single Evaluate call, ie:

    Sub Example()
    With Range("A140",Range("A140").End(xlDown).End(xlToRight))
        .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
    End With
    End Sub
    It's not clear what appears after this range of interest - if anything - you might want to revisit the selection process as you could end up processing a very big range (unintentionally)

  4. #4
    Registered User
    Join Date
    02-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: trim

    perfect, thanks very much!

+ 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