+ Reply to Thread
Results 1 to 14 of 14

Manipulating strings in VBA

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Manipulating strings in VBA

    Hi all,

    I am only asking this because I am not completely sure about how VBA can manipulate strings. The following is a string that is in a cell:
    Engine, turbine and power transmission equipment manufacturing [3336] (v53384920)
    What I want to do is transfer only the v-series number (v53384920) to a different cell. The cell destinations are not important as I can do that without a problem. Its the removal of all the fluff & stuff that I don't need that is giving me problems. Any help is always appreciated.

    Regards:
    Last edited by Mordred; 10-27-2010 at 01:36 PM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,607

    Re: Manipulating strings in VBA

    Is the number always the same number of characters? Is it always in brackets? Is it always at the end?

    If yes to all of these, try this:

    =MID(A1,FIND("(",A1)+1,LEN(A1)-FIND("(",A1)-2)

    Or this, if it's always in brackets:

    =MID(A1,FIND("(",A1)+1,FIND(")",A1)-FIND("(",A1)-1)

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Manipulating strings in VBA

    For always at the end and any length (up to 50 characters) after the last space:

    =TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",50)),50))

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Manipulating strings in VBA

    Hi Stephen and thanks, the number of characters may or may not change and I have no control over that. Phrom my own observations, the characters are always enclosed in brackets but they may not always be at the end ov the string.

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,607

    Re: Manipulating strings in VBA

    In that case you should be able to use the second formula (assuming you have no brackets anywhere else).

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,218

    Re: Manipulating strings in VBA

    Another way if always at the end, search isn't case sensitive.

    =MID(A1,SEARCH("(v",A1,1),255)

    Cheers
    Last edited by Marcol; 10-27-2010 at 01:03 PM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  7. #7
    Registered User
    Join Date
    10-26-2010
    Location
    Tennessee
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Manipulating strings in VBA

    Nm. Additional requirements were named while I was writing
    Last edited by PatrickSwayze; 10-27-2010 at 12:47 PM.

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Manipulating strings in VBA

    I have attached a phile to show the results of a program I have created (with this site's help of course). The program phinds the changes and then places them on a sheet like in the attached phile. It lists the original (ours) v-series and the new (the source) v-series. I don't want the user to manually make changes because there may be errors.
    Attached Files Attached Files

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,607

    Re: Manipulating strings in VBA

    Well, have you tried the approaches suggested to see if they work?

    EDIT:

    =IF(ISNUMBER(FIND("(",C2)),MID(C2,FIND("(",C2)+1,FIND(")",C2)-FIND("(",C2)-1),"")
    Last edited by StephenR; 10-27-2010 at 12:55 PM.

  10. #10
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Manipulating strings in VBA

    Stephen's formula adapted to get round other ( in the string:

    =MID(A1,FIND("(v",A1)+2,FIND(")",A1,FIND("(v",A1)+2)-FIND("(v",A1)-2)

    Dom

  11. #11
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Manipulating strings in VBA

    Hi Stephen, I did and they work, I was hoping to have this as code though rather than a function on the spreadsheet side of things.
    =IF(ISNUMBER(FIND("(",C2)),MID(C2,FIND("(",C2)+1,FIND(")",C2)-FIND("(",C2)-1),"")
    works great though and thank you.

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,218

    Re: Manipulating strings in VBA

    I'm behind the curve here!!!!

    Try ths

    =MID(A1,SEARCH("(v",A1,1),FIND(")",MID(A1,SEARCH("(v",A1,1),255),1))
    Last edited by Marcol; 10-27-2010 at 01:02 PM.

  13. #13
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,607

    Re: Manipulating strings in VBA

    I suppose v could be elsewhere in the string though.
    Please Login or Register  to view this content.

  14. #14
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Manipulating strings in VBA

    I just called a quick meeting with one user and 2 potential users of this workbook. They all agreed to autofilling the formula down the sheet after the initial code (that finds the changes) has been run. Thanks all for all of your help on this and I think I got everyone regarding rep points. Again, thanks

    Regards.

+ 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