+ Reply to Thread
Results 1 to 4 of 4

Trim data in colum A throughout multiple sheets

  1. #1
    Registered User
    Join Date
    04-18-2012
    Location
    Vienna, Va
    MS-Off Ver
    Excel 2003
    Posts
    5

    Trim data in colum A throughout multiple sheets

    I am trying to get Column A to trim down because of extra spaces in it. I need to do this across 29 sheets and can not get the formula to work.

    Here is what I am trying to use




    Sub TrimSpaces()

    Application.ScreenUpdating = False
    For Each sh In Worksheets
    If sh.Name <> "Emall" And sh.Name <> "FPDSNG" Then
    sh.Activate
    Range("A2:A40000").Select

    For Each cell In Selection
    cell.Value = Trim(cell.Value)
    End If
    Next sh
    Application.ScreenUpdating = True

    MsgBox "Data has been updated. "

    End Sub




    I get a "Compile Error: End If without Block If


    Any help would be greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Trim data in colum A throughout multiple sheets

    you appear to be missing "next cell" before "end if"

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Trim data in colum A throughout multiple sheets

    Hi bmuum3

    Not trying to solve you problem because NickyC did that.

    I’m just curious about your range selection i.e. “A2:A40000”.

    If you are setting it that big just to be sure you “capture” all values you could instead try to set the range dynamically.

    Range(“A2:A” & Range(“A” & Rows.Count).End(xlUp).Row) will go from the bottom and upwards to the first value in the A column. If the first value found is at row 1500 (going from the bottom and upwards) excel will then interpret
    Range(“A2:A” & Range(“A” & Rows.Count).End(xlUp).Row) as Range(“A2:A1500”)

    So you could rewrite your macro a bit changing

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    Alf

    Ps Always wrap your code or the moderators will be upset!

  4. #4
    Registered User
    Join Date
    04-18-2012
    Location
    Vienna, Va
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Trim data in colum A throughout multiple sheets

    Thanks for all the help

    Alf, thanks for the heads up.

+ 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