+ Reply to Thread
Results 1 to 27 of 27

Extracting up to 3 substrings from a string

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Extracting up to 3 substrings from a string

    Hi, I have some really messy and large dataset which I need to extract up to 3 substrings from (max) following the occurrence of specific text.

    Within the string, whenever "condition=" or "condition " occurs, I need to extract the 1st, 2nd and 3rd substring (if present) appearing directly after and before the next space within the string.

    I have attached a spreadsheet with an example of what I am trying to achieve.

    Many thanks for any help with achieving this.
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Extracting up to 3 substrings from a string

    Hi darls15,

    Try the below code based on your sample file

    Please Login or Register  to view this content.
    Last edited by nankw83; 07-24-2020 at 04:21 AM. Reason: Small correction in the code
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,176

    Re: Extracting up to 3 substrings from a string

    @ nankw83...Great code

    @ darls15...Another option...
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  4. #4
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    Another alternative using "Regular Expressions" is:

    Please Login or Register  to view this content.
    Last edited by Haluk; 07-24-2020 at 08:18 AM.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Extracting up to 3 substrings from a string

    What does [=| ] suppose to do?
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    to fetch either "condition=....." or "condition ....."

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Extracting up to 3 substrings from a string

    No, that is either "=" or "|" or a space.

    If you want "OR", it should be (=| )

  8. #8
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    Ahhh.... my bad. It shouldn't be in "group".

    Thx Jindon.... I've edited my code in message #4

  9. #9
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    Hi nankw83,
    Thanks for the quick reply. I've tried your code and am getting a "Run-time error '13': Type mismatch" on this row...
    Range("B2").Resize(UBound(a), 3) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(2, 3, 4))
    Any idea what might be the issue?

  10. #10
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @ sintek

    I'm so in awe of what you have provided here. I tried your code on my 130,000+ row data and it ran perfectly and not to mention how quickly. Thank you so much, you have saved me so much time, frustration and formula!

    Also, I've noticed that in my data there are instances where the string that is extracted has double quotes around it. If it's not too much trouble, is it possible to add to this code to still extract everything between the double quotes, including any spaces if they exist, and then remove the double quotes in the result? My example is below...

    condition="Exception_id12948" >> Exception_id12948
    condition "Client MediumAllows_id4162" >> Client MediumAllows_id4162
    condition="Year 09" >> Year 09

  11. #11
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Extracting up to 3 substrings from a string

    Quote Originally Posted by darls15 View Post
    Any idea what might be the issue?
    Does the error occur in your sample file as well or just your original file only ?

    Anyway, try the revised below code which I have added couple of "IF" condition to ignore wrapping quotes ...

    Please Login or Register  to view this content.

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,176

    Re: Extracting up to 3 substrings from a string

    Perhaps This?
    Please Login or Register  to view this content.
    Last edited by sintek; 07-25-2020 at 02:59 AM.

  13. #13
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Extracting up to 3 substrings from a string

    I don't know about speed for VBA

    Maybe try

    Please Login or Register  to view this content.
    Keep value in Array is a lot faster
    Last edited by Bo_Ry; 07-25-2020 at 05:37 AM.

  14. #14
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    @darls15;

    Just out of curiosity... did you try the code in message #4?

  15. #15
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @Haluk, thanks and my apologies for not getting back sooner. Yes, I have tested it on my master data. I'm getting a 'Run-time error '6': Overflow' and it stops at 'NoA = Range("A" & Rows.Count).End(xlUp).Row'. Any idea what the cause of this could be? Thanks

  16. #16
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @nankw83, the error only occurred in my original file, the sample file was fine so there was something I was obviously missing there. Anyways, I have just re-tested test_v2 and no error which is awesome. The double quotes are removed, however the entries that have a space only result in the first work being extracted. Is there a way to extract everything between the double quotes or is this getting a bit tricky? In the example below, only the word Client and Year were extracted.

    condition "Client MediumAllows_id4162" >> Client MediumAllows_id4162
    condition="Year 09" >> Year 09

  17. #17
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    In my code, you may need to change declaration type of the "Integer" type variables to "Long"

  18. #18
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @sintek, I've just tested sintek and got a 'Run-time error '9': Subscript out of range on this line...
    If Str(ii + 2) Like "*""" Then

    I did a bit of a test with my original data and it seems to be failing on rows that have the double quotes and doesn't include a space as per below...
    Doesn't work - Define condition "X_AdminNetworks"
    Works - Define condition "X_AdminNetworks Global"

  19. #19
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @Bo_Ry, thanks for this. I tested ThreeSub and no errors. I did however forgot to include in my example rows that don't have have the word "condition" which resulted in the entire row being extracted. For example...

    force_extension("defining.original",".pname:P_2101_notify") >> (the result should be blank here)

    Also, the double quotes were removed, however the entries that have a space only result in the first work being extracted. For example...

    Define condition "X_AdminNetwork Global" >> X_AdminNetwork (should be X_AdminNetwork Global)

    Thanks again for your help

  20. #20
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Extracting up to 3 substrings from a string

    Does this macro do what you want...
    Please Login or Register  to view this content.
    EDIT NOTE: I noticed a couple of your example text strings had a "free floating" individual quote mark, for example this one (highlighted in red)...
    condition BlockSites_id2451 defined.centerOpen", condition P_584_categories Windows Categories exeptions condition Social_Networking
    Is that a typo created when your made your example file or could your data really have individual quote marks like that? If it was a typo, then my above code should work find for you; however, if that is a possibility, then I seriously doubt any coded solution that ignores spaces between quote marks will be possible.
    Last edited by Rick Rothstein; 07-25-2020 at 06:47 PM.

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Extracting up to 3 substrings from a string

    darls15
    Try
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by jindon; 07-25-2020 at 10:16 PM.

  22. #22
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,147

    Re: Extracting up to 3 substrings from a string

    @Jindon; Very nice pattern

  23. #23
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Extracting up to 3 substrings from a string

    This should work.
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @Rick Rothstein, thanks this works perfectly and yes the "free floating" quote was a typo!

  25. #25
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @Bo_Ry, yes it does work perfectly, thank you!

  26. #26
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    @jindon, works perfectly, thanks!

  27. #27
    Registered User
    Join Date
    06-26-2014
    Location
    Queensland, Australia
    MS-Off Ver
    2016
    Posts
    35

    Re: Extracting up to 3 substrings from a string

    Thank you all very much for your time and input. I'm very appreciative of all your efforts to help with my problem here and now have multiple working solutions. I'm very excited to be manipulating my original data to output results exactly how I needed it

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replace multiple substrings in string
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-16-2019, 05:44 AM
  2. [SOLVED] VBA to Find Multiple Substrings Within a String
    By learning_vba in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-06-2014, 07:35 AM
  3. [SOLVED] VBA substrings in string
    By mikejclark in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-28-2013, 08:36 AM
  4. Excel 2007 : Finding substrings in a string
    By kkerr in forum Excel General
    Replies: 4
    Last Post: 06-19-2012, 11:57 AM
  5. Changing multiple substrings within a string
    By RogueArchon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2012, 12:57 PM
  6. Substrings from String
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-23-2010, 05:08 PM
  7. Separtaing A String Into Substrings.
    By ExcelTip in forum Tips and Tutorials
    Replies: 0
    Last Post: 07-10-2005, 01:27 AM

Tags for this Thread

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