+ Reply to Thread
Results 1 to 8 of 8

Modify macro so it only performs TRIM if leading space is present

Hybrid View

OhioRuss Modify macro so it only... 04-17-2018, 03:54 PM
alansidman Re: Modify macro so it only... 04-17-2018, 04:31 PM
OhioRuss Re: Modify macro so it only... 04-17-2018, 04:52 PM
OhioRuss Re: Modify macro so it only... 04-17-2018, 04:54 PM
OhioRuss Re: Modify macro so it only... 04-17-2018, 04:58 PM
OhioRuss Re: Modify macro so it only... 04-17-2018, 04:38 PM
Greg M Re: Modify macro so it only... 04-17-2018, 04:47 PM
Greg M Re: Modify macro so it only... 04-17-2018, 05:00 PM
  1. #1
    Registered User
    Join Date
    12-09-2017
    Location
    Ohio
    MS-Off Ver
    Excel 2013 (home/student)
    Posts
    62

    Modify macro so it only performs TRIM if leading space is present

    I have a macro that trims leading spaces from every cell in a worksheet. In large spreadsheets, it is slow because most cells don't have leading spaces and trimming them is wasted time. How would I modify the macro so that it firsts tests to see if a leading space is present in a cell, and only trimming those where one is? In concept it's simple, eg. read the first character in a cell, iff it is a space, then replace the cell value with the trimmed cell value. Just don't know how to write it in code. Here is my existing macro:
    Sub RemoveLeadingSpace()
    For Each cel In ActiveSheet.UsedRange
    cel.Value = Trim(cel.Value)
    Next cel
    End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Modify macro so it only performs TRIM if leading space is present

    If you have a lot of formulas in your worksheet, changing the calculation to manual may speed up the macro significantly. At the end of the macro, change the calculation back to Automatic. Additionally, at the beginning of the macro, set the screen updating to false and then back to true at the end. This will avoid the repainting of the screen with every trim that is performed.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    12-09-2017
    Location
    Ohio
    MS-Off Ver
    Excel 2013 (home/student)
    Posts
    62

    Re: Modify macro so it only performs TRIM if leading space is present

    I tried this. It did what is was supposed to (delayed the screen update to the end), but didn't noticeably speed things up. I'd still like to try a modification along the line I suggested originally.
    Sub RemoveLeadingSpace()
    With Application
            .ScreenUpdating = False
        End With
    
    For Each cel In ActiveSheet.UsedRange
    cel.Value = Trim(cel.Value)
    Next cel
    
    With Application
            .ScreenUpdating = True
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    12-09-2017
    Location
    Ohio
    MS-Off Ver
    Excel 2013 (home/student)
    Posts
    62

    Re: Modify macro so it only performs TRIM if leading space is present

    Thanks, Greg, just spotted your reply. Sounds like what I'm looking for. Will try it and report.

  5. #5
    Registered User
    Join Date
    12-09-2017
    Location
    Ohio
    MS-Off Ver
    Excel 2013 (home/student)
    Posts
    62

    Re: Modify macro so it only performs TRIM if leading space is present

    Worked like a charm, Greg. Thanks again. I will mark the thread Solved.

  6. #6
    Registered User
    Join Date
    12-09-2017
    Location
    Ohio
    MS-Off Ver
    Excel 2013 (home/student)
    Posts
    62

    Re: Modify macro so it only performs TRIM if leading space is present

    Thanks, Alan. These spreadsheets have no forumulas - they are all straight text (they are for genealogy data, not calculation). But I'll try the screen updating suggestion.

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Modify macro so it only performs TRIM if leading space is present

    Hi there,

    See if the following code does what you need:

    
    
    Sub RemoveLeadingSpace()
    
        Dim rCell As Range
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
            For Each rCell In ActiveSheet.UsedRange.Cells
    
                If Left$(rCell.Value, 1) = " " Then
                    rCell.Value = Trim(rCell.Value)
                End If
    
            Next rCell
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Modify macro so it only performs TRIM if leading space is present

    Hi again,

    Many thanks for your very prompt feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Leading Zero and Right Trim question
    By eeps24 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-16-2016, 03:50 PM
  2. [SOLVED] TRIM leading character
    By lemonap618 in forum Excel General
    Replies: 3
    Last Post: 01-20-2016, 11:01 PM
  3. [SOLVED] trim string before the space - macro request.
    By missy22 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-09-2014, 05:11 AM
  4. Replies: 4
    Last Post: 02-07-2012, 07:47 AM
  5. Trying to trim a trailing space char and the Trim function isn't working
    By Psychochook in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2007, 11:28 PM
  6. trim leading spaces
    By captain bob in forum Excel General
    Replies: 2
    Last Post: 08-03-2006, 10:00 AM
  7. [SOLVED] trim leading spaces
    By hawat.thufir@gmail.com in forum Excel General
    Replies: 10
    Last Post: 05-11-2006, 04:25 PM

Tags for this Thread

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