+ Reply to Thread
Results 1 to 12 of 12

VBA Trim

  1. #1
    Registered User
    Join Date
    09-04-2008
    Location
    Singapore
    Posts
    24

    VBA Trim

    Hi I would like to have a simple basic VBA trim function with no complications at all. Just a simple trim vba code like:

    =trim( Selected/Active cells)

    But I just couldn't think of how to do it, even with recording.

    Thanks

  2. #2
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    This is about as simple as you can get...

    Sub VBATrim()

    ActiveCell = Trim(ActiveCell)

    End Sub

  3. #3
    Registered User
    Join Date
    09-04-2008
    Location
    Singapore
    Posts
    24
    lol Cool thanks!

    Thats just what I need.

    Hmm ok now a little bit of challenge to add to this function. I don't know if its just me but any of u guys knew that when you do a manual trim on excel itself, it will not remove leading 0's infront of any string.

    However, if use a trim from the VBA function, the 0's would be removed.

    Any solutions?

    Thanks

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Trim doesn't remove leading zeros; it removes leading and trailing spaces. See Help.

    The WorksheetFunction.Trim and VBA Trim functions are different. Help explains both.
    Last edited by shg; 12-17-2008 at 12:24 AM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    I've passed an integer to the 'Trim' function in the 'Immediate' window and it did remove the leading zeros. I didn't find any specific mention of this in either 'Help File' Reference.

    However, if the ActiveCell is formatted as a number, the leading zeros would be removed on input in the sheet. If formatted as 'Text', a string value is passed to the VBA function thereby alleviating the concern for dropping zeros.

    Are you having a practical issue that I'm not understanding?

  6. #6
    Registered User
    Join Date
    09-04-2008
    Location
    Singapore
    Posts
    24
    Hi once again, if I am not wrong, the default cell formats would be a General Format, so lets say I did a trim with the VBA function, it will remove the leading zeroes.

    Well of course I can change it into text format first before I do anything but this means that I need to change it for every single worksheet that I need to work on. I need to save as much time as possible.

    Thanks

    PS: Hey Goos, the trim function seem to work only on one cell. Lets say I would like to select multiple or a whole column/row, is it possible to edit?
    Last edited by lazenca; 12-17-2008 at 01:11 AM.

  7. #7
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    I think your situation must be that the data has Leading Apostrophes. Without converting the cells to anything other than 'General', you can implement a numeric check, as well as cycle through all cells in a selection this way:
    Please Login or Register  to view this content.
    I've tried this with a few different types of cells and I think it works as you'll need. Just understand that if there is a function or formula in the cell, you'll wind up getting the results' values.
    Last edited by Leith Ross; 12-17-2008 at 10:26 PM. Reason: Added Code Tags

  8. #8
    Registered User
    Join Date
    09-04-2008
    Location
    Singapore
    Posts
    24
    Hey goos, it works! =)

    Well it will be best if you can explain to me the syntax and logic behind this coding of yours cuz I am still new at VBA and I wanna get as much exposure as possible, its wonderful =)

    Thanks

  9. #9
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    I'd love to. If you mark this post as resolved, I'll be happy to do a private thing so we don't pollute the postings. I'll send you a private message and you can send me an e-mail if you like. ~R

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Please keep the exchanges in the forum -- that's the point of a forum.

  11. #11
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    Well, okay. I'll comment the following:
    Please Login or Register  to view this content.
    In the line that says 'rngCell = "'" & Trim(CStr(rngCell))', t'he ampersand '&' is basically a textual operator, like '+' or '*' are mathematical operators. It says 'Take whatever is in front of me, and stick it to whatever is after me.

    The 'For Each' and 'Next' lines are wrappers which work together to do everything in between the two lines multiple times.

    Not sure if this helped. If not, just let me know.

  12. #12
    Registered User
    Join Date
    09-04-2008
    Location
    Singapore
    Posts
    24
    Please Login or Register  to view this content.
    Hmm I am not quite sure about this part. You see, when there are leading zeroes in the cell, it is only visible when its cell format is in 'Text'. But for this case, it asked if it is a 'Number'?

    Explain please.

    Thanks alot

+ 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