+ Reply to Thread
Results 1 to 27 of 27

String Manipulation - Split into multiple variables

  1. #1
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    String Manipulation - Split into multiple variables

    Hello,

    I have a string that is pulled in from a 3rd part app. It ends up looking like this:

    1234-56JUL10 2345678JUL10 3456789JUL10 876543JUL10 987654JUL10

    It is all one string.... I need this to be able to have a variable that can be updated to the position in the string. So for example I need the 3rd portion is there any way to specifically pull the 3rd section of the string?

    To explain a little, each of these are basically "claims" and I need to be able to check them one at a time which I would use a counter variable like d = d + 1. I know I could pull this value into a cell, use text to columns and then just use Cell to keep moving over 1 column until I found what I needed, but is there a way in VBA without pasting the values into a cell and using text to columns?

    Edit: See end of thread for more info. I can't easily use a function.
    Attached Files Attached Files
    Last edited by Dulanic; 09-09-2010 at 01:58 PM. Reason: Solved....again

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: String Manipulation - Split into multiple variables

    Dulanic,

    Detach/open workbook SplitData - Dulanic - EF744090 - SDG12.xlsm and run macro SplitData.

    You only gave one example of the string.

    I assume that all your data in column A, except for the beginning and ending spaces, contains 24 space characters between the strings you need.
    Attached Files Attached Files
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: String Manipulation - Split into multiple variables

    Hello Dulanic,

    This macro is a User Defined Function that will allow a variable number of spaces before and after the claim numbers. Copy this code into a standard VBA Module in your workbook.
    Please Login or Register  to view this content.

    Macro Example
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: String Manipulation - Split into multiple variables

    Leith Ross,

    Very nicely done.

    I just received:
    The book 'Sams Teach Yourself Regular Expressions in 10 Minutes' by Ben Forta

    Can you suggest any other books on the subject?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: String Manipulation - Split into multiple variables

    Hello Stanley,

    Sorry, I haven't got any books on Regular Expressions, nor have I read through any. I have learned it through using it. Most of the information I have seen has been for Java, or PHP. There seems to be little specifically for VBScript RegExp. I will have to check out the Sams publication. Thanks for information.

  6. #6
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: String Manipulation - Split into multiple variables

    Leith Ross,

    You are very welcome.

    How would I, you, change your pattern, to work with a long string that does not end in a space or spaces?

    Please Login or Register  to view this content.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: String Manipulation - Split into multiple variables

    Hello Stan,

    I am not sure what would need to be changed. The pattern operator \s+ will look for one or more spaces. The operator (.*) will hold the remainder of the string. You could change it to \s* which would look for zero or more spaces. Did you want to test for something else?

  8. #8
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: String Manipulation - Split into multiple variables

    Leith Ross,

    If I use your function with Dulanic's original string (without the " marks):

    " 8927-27JUL10 4601107JUL10 1783421JUL10 3511826JUL10 9886-09JUL10"

    And use the formula:
    =GetClaim(A1, 5)

    The result is blank.


    If I add a trailing space to the Dulanic's original string (without the " marks):
    " 8927-27JUL10 4601107JUL10 1783421JUL10 3511826JUL10 9886-09JUL10 "

    And use the formula:
    =GetClaim(A1, 5)

    The result is:
    9886-09JUL10

  9. #9
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Hello,

    Thank you all for the code, I appreciate it. Is it possible to do this without posting the data to the workbook? These "claims" do not need to be seen by the user as it is going to be part of a larger macro. This macro interfaces with another app and checks each claim to see if it matches the rest of the data. This string is never shown to the user nor is it placed into the excel workbook, it is purely stored as a variable. I guess I could have the macro put the claims on a worksheet, but then I think it may just slow down the macro, this macro already takes about an hour to run since it interfaces with another program. Hope that makes sense?

    Here is a very dumbed down basic example of what I am doing:

    Please Login or Register  to view this content.
    I also see what Stan was saying. My string can be variable, it may or may not end in spaces. It seems if I use ...

    Please Login or Register  to view this content.
    This seems to always work
    Last edited by Dulanic; 09-02-2010 at 07:32 AM.

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

    Re: String Manipulation - Split into multiple variables

    Another UDF might be
    Please Login or Register  to view this content.

    Or called in VBa
    Please Login or Register  to view this content.
    will return the first value
    Please Login or Register  to view this content.
    Returns the second
    Please Login or Register  to view this content.
    Uses "," as the separator instead of the default " " (Space).

    Hope this helps
    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.

  11. #11
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    I think that might do it. Just use a variable so I can do Your UDF doesn't work for me, however, I think I can twist Leith Ross's into doing what I need just by setting it like so:

    Please Login or Register  to view this content.
    to roll through each of them until the result is what I need or I hit a certain number.

    I appreciate all the help and will mark solved once I get a chance to test and implement.
    Last edited by Dulanic; 09-02-2010 at 09:11 AM.

  12. #12
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Not quite there, I am getting closer This below code using Leith's code. However, it stops 1 claim prior to the last claim. I tried

    Please Login or Register  to view this content.

    This throws an error. I was hoping to add a leading and trailing space to force it to work. Using just adding the trailing space didnt seem to change the results.

    Using =GetClaim($A$1 & " ",1) just throws a #VALUE result.

    The preferable way for this to work for me would be:

    Please Login or Register  to view this content.
    I can do this to force it, but then I just keep adding spaces each time it's run.

    Please Login or Register  to view this content.
    Last edited by Dulanic; 09-02-2010 at 09:41 AM.

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

    Re: String Manipulation - Split into multiple variables

    It seems to work for me.

    See attached workbook
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Odd don't know why it wasnt working for me. OK so leading that into my other question, is it possible to fit this into VBA only with no reference to a cell as I indicated above?

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

    Re: String Manipulation - Split into multiple variables

    I'm not sure what you are trying to do

    However try this modification to the UDF and apply it in VBa as explained in post #11
    Please Login or Register  to view this content.
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Sorry if I did not explain well enough. My goal is to avoid using the worksheet itself to do this, the string is originally pulled in to a variable via code to grab it from the 3rd party app. I want to try and avoid actually putting the value on the worksheet because noone needs to see it until it finds a match, does this make sense? Basically is there a way to make it allow the 1st variable for GetClaimFrom allow a variable input instead of a range input?

    Please Login or Register  to view this content.
    Last edited by Dulanic; 09-02-2010 at 11:01 AM.

  17. #17
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Got it, sorry for all the extra work and me not explaining it that well. This was all I needed, i just haven't used Split all that much and didn't realize all I needed to do was provide the delimiter and the position after. I do have to admit the (v) for the position afterword kind of throws me off and not sure how that works out. I mean I understand what it did, just the layout of it doesnt make sense to me.

    Please Login or Register  to view this content.
    Last edited by Dulanic; 09-02-2010 at 11:35 AM.

  18. #18
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: String Manipulation - Split into multiple variables

    Use the builtin facilities of vba

    If you split a string into an array you can filter it and check the filtering result. No need for a Do.. or For.. loop.

    Please Login or Register  to view this content.

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

    Re: String Manipulation - Split into multiple variables

    Try this in a blank sheet
    Please Login or Register  to view this content.

    This, based on your sample string, will split your string then divide again into Code and date (as a string).

    WorksheetFunction.Trim() is used rather than just Trim() because it will remove all extra spaces, not just the leading and trailing spaces.

    Hope this helps

  20. #20
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: String Manipulation - Split into multiple variables

    Hello Dulanic,

    Here is the corrected version of my macro. The fix was to add a space to the end of the Claims and change Claims from a Range to a String.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 09-02-2010 at 04:17 PM.

  21. #21
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: String Manipulation - Split into multiple variables

    if string c01 contains all claims as 1 string you can get the third claim using:

    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    OK so I am reopening this thread as I am finding what I was doing isn't working.... I was using this:

    Please Login or Register  to view this content.
    v is a variable interger. The problem comes in if it is not able to find a match it tried to keep going to let's say it does v as 6. This results in an error since 6 is not a valid option since it only split 5 claims. Is there a way to prevent this error?

    I guess what I am wants to find out is can I get a count for "Claim" since it breaks down into multiple variants and I want to know how many? Like there is Claim(0) through Claim(5) so I want to know the highest variant is 5 so I can throw a catch to stop at 5. Sometimes 5 mights be 4 or 7....

    Thanks to those for functions, but functions arent going to really do what I need since I am trying to get variables purely in VBA and not involve a worksheet.
    Last edited by Dulanic; 09-09-2010 at 12:24 PM.

  23. #23
    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: String Manipulation - Split into multiple variables

    What would you want to return if it were beyond the last value -- a null string?

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  24. #24
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Quote Originally Posted by shg View Post
    What would you want to return if it were beyond the last value -- a null string?

    Please Login or Register  to view this content.
    Looks to be working because then I could toss in....

    Please Login or Register  to view this content.

  25. #25
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: String Manipulation - Split into multiple variables

    I answered your question before posted it:

    Please Login or Register  to view this content.



  26. #26
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: String Manipulation - Split into multiple variables

    Quote Originally Posted by snb View Post
    I answered your question before posted it:

    Please Login or Register  to view this content.
    I appreciate the help snb. While I saw your solution, it did not fit into what I needed. Sorry if I didn't explain it well. While I knew the (1) etc... could be variable to do what I needed, I had no idea what the last claim technically was. So what I needed I had to have it continue until it hit no claim and then do something else. Since the amount of claims were variable split would throw an error if I went all the way split with (6) but there are only 5 claims.

    I really do appreciate your help however and you have been helpful to me in the past also.

  27. #27
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: String Manipulation - Split into multiple variables

    Please Login or Register  to view this content.
    This means: it returns the last item form string c01, that has been split by a space. This is a variable way of returning values and it can't return an error.

    e.g
    c01 is string "aa bb gg tt uu"
    last_item will return 'uu', using
    Please Login or Register  to view this content.

+ 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