+ Reply to Thread
Results 1 to 18 of 18

macro to trim only text in mixed format spreadsheet

  1. #1
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Question macro to trim only text in mixed format spreadsheet

    I have spreadsheets that contain columns of text, numbers, function of those numbers, etc. I would like to develop a macro that will search the spreadsheet for any cells that contain text and trim the string within. I've found macros already written that trim all cells in a spreadsheet, but they change cells using formulas to value only. I need to keep the formulas active. Here is a macro I found in a forum that trims a column of text but you have to define the column and range each time:

    Please Login or Register  to view this content.
    I don't have enough vba knowledge yet to simply make the macro apply to any one text column by just highlighting a range, OR enough know-how to tell the macro to go from one column to the next to search for text to trim. Please let me know how to edit it to achieve my goals. Thank you!
    Last edited by burnettec; 12-23-2012 at 04:47 PM.
    burnettec

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: macro to trim only text in mixed format spreadsheet

    attach a sample file with currend tabḷe and desired result
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    Thank you for reponding.
    I have attached a test file in which I'd like to trim the text in columns H & J in one macro without affecting the format of the rest of the spreadsheet. The ("manual") macro I've entered has not been applied to those columns yet.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: macro to trim only text in mixed format spreadsheet

    where is the desired result ?

  5. #5
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    Hi, patel45. The desired result is columns H & J without spaces at the beginning or end. I can do it individually on each column but want a macro for both columns or any other text columns I add to the spreadsheet. The attachment I sent earlier doesn't have any macros applied. Visually, the original file and desired result will look the same because you can't see spaces (present or deleted) at the end of text.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    @burnettec,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    HTH
    Regards, Jeff

  7. #7
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    OK, thank you. I actually read that after the fact. My apologies. Would you like me to "solve" the post and restart it fresh. The macro code is actually in the attachment later on in the replies.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    No you can just go to post #1 and add the tags.

  9. #9
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    Done! Thanks for guidance.

  10. #10
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    Is the best way to approach my issue to edit the current macro to incorporate a loop that searches for columns of text, trims them, goes to the next column, etc. Can someone get me started on how to write that? It's an if/then/else-thing?

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    Quote Originally Posted by burnettec View Post
    The desired result is columns H & J without spaces at the beginning or end.
    Am I correct in seeing that these two columns only have text and these are the two columns you want to trim?

  12. #12
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    yes. those are the only text columns for now... i will add more later as i get more data and will want to trim all the columns in the spreadsheet for reasons I won't detail other than i may need to compare sells to ensure matching text.

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    Here is a macro written by AlphaFrog which I modified slightly.

    With this macro it will allow you to click on the first cell in the column you want to trim and will select the last cell in that column.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    Thank you for your suggestion but I got a runtime error 9, subscript out of range. I assumed the message box implied i could click on any cell i wanted to begin with? Not just the first cell of the column?

  15. #15
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    Did you update the sheet name to the proper name?

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    No. Duh. I did make sure i was on the first sheet but forgot quotes mean text. Sorry. Just finished an Excel VBA class. Will test again.

  17. #17
    Registered User
    Join Date
    12-23-2012
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: macro to trim only text in mixed format spreadsheet

    That worked, thanks. Thanks to alphafrog, whoever you are. This is not exactly my end goal for a macro, but it is much better than having to enter where to start cleanup and I can work with it as far as looping across the spreadsheet. For that matter it may be better to retain some control about where to start cleanup instead of letting loose with a super-macro until I better understand vba. Don't want to mess up data for the sake of proving i can write (or can't write) code. --- will consider this thread "solved" (when I figure out how to edit that status...)

  18. #18
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: macro to trim only text in mixed format spreadsheet

    Glad it works out for you and happy to have helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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