+ Reply to Thread
Results 1 to 9 of 9

Excel VBA - Split String

  1. #1
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    Excel VBA - Split String

    Hi,

    I have a string that can be passed through in various different formats:

    "CR 123456 DB 654321 EUR"
    " CR 123456 DB 654321 EUR "
    "DR123456CR654321"
    "DR 123456 CR 654321 GBP"
    " DR 123456 CR654321 GBP "

    etc etc

    I am basically looking to pull out the 6 digit DR number and 6 digit CR number from the strings regardless of the passed in format.

    So I would have something like GetCreditAccount = "123456" and GetDebitAccount = "654321"

    Is anyone able to help?

    Thanks
    McCrimmon

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Excel VBA - Split String

    Hi,

    You can use these functions

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please take time to read the forum rules

  3. #3
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    Re: Excel VBA - Split String

    Hi

    Thanks for your response, however, that only works when the CR account is the first account.

    Sometimes the string will be passed with the DR account first such as the below examples:

    "DR123456CR654321"
    "DR 123456 CR 654321 GBP"
    " DR 123456 CR654321 GBP "
    Any thoughts?

    Thanks

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Excel VBA - Split String

    These are closer.

    I wonder, in your first post you have both DB and DR for the same thing, can the format be like this or is it an error?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    Re: Excel VBA - Split String

    Apologies.

    They should all be DR and CR.
    Just a typo on the example data.

  6. #6
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    Re: Excel VBA - Split String

    Thanks for the help.

    Seems to work a treat.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,963

    Re: Excel VBA - Split String

    An alternative, if more long winded version:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  8. #8
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    Re: Excel VBA - Split String

    A further question though and I'm not trying to over complicate things, I'm just making sure I add in as much control as I can to the function.

    Is there anyway to test that each account is 6 numeric digits.

    For example if I pass in a string of "DR12345CR654321" (the DR account missing a digit) then I would be looking for this to only show 12345 and then error due to only having 5 digits rather than 6.

    The current process would make this DR account 12345C, which is incorrect.

    Any suggestions from anyone is greatly appreciated.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Excel VBA - Split String

    or maybe even:

    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