+ Reply to Thread
Results 1 to 8 of 8

Trim Text in VBA

  1. #1
    Registered User
    Join Date
    02-16-2010
    Location
    Ohio
    MS-Off Ver
    Excel 2007
    Posts
    3

    Trim Text in VBA

    Hello,

    I have a small Excel VBA program that pulls data from our company database.
    I use this to collect information about orders that have been placed.
    The decriptions of our inventory within our accounting software usually go something like this: [2/24] Small Red Rose
    The [2/24] stands for 24 pieces per case and 2 pieces per inner pack.
    My question is... can I trim off the text '[2/24]' within VBA?
    I need to create quotes, labels and other things that use these descriptions and don't need to include the information with the brackets.

    Obviously within VBA everything is done with variables, such as strDESC for the description.

    Thank you for any help you can provide.
    Last edited by Dracana; 02-16-2010 at 03:12 PM.

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

    Re: Trim Text in VBA

    If this info appears at the top of the string (if at all) then perhaps:

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 02-16-2010 at 02:43 PM. Reason: forgot to add variable declarations

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Trim Text in VBA

    You can actually fix the entire column of data all at once, with a line of code something like this:

    Please Login or Register  to view this content.

    Notice we are replacing everything inside the brackets PLUS the brackets PLUS one space afterward with nothing.

    You could, of course, do the replace string by string within the macro proper, but you could do it all at once as shown.
    Last edited by JBeaucaire; 02-16-2010 at 02:49 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    02-16-2010
    Location
    Ohio
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trim Text in VBA

    Thanks,

    One question though. What do I do if the brackets are not there to begin with. Not every description has brackets, just most of them. Will the replacement code error and just leave me with an error code?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Trim Text in VBA

    I just tested it on a column with no brackets and I got no error message.

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Trim Text in VBA

    Either sets of code will work just fine whether the brackets are there or not
    Richard Schollar
    Microsoft MVP - Excel

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Trim Text in VBA

    What do I do if the brackets are not there to begin with.
    Testing is a good way to find out
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    02-16-2010
    Location
    Ohio
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trim Text in VBA

    I just tried it in my program and it worked beautifully!
    Thank you 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