+ Reply to Thread
Results 1 to 26 of 26

How to get substring in a major string with VBA

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    How to get substring in a major string with VBA

    Hi

    I have a few sample strings like bellow:


    1: "in-Training and Development Officer Pakistan"
    2: "Training and Development Officer Pakistan-in"
    3: "Training and Development Officer in-Pakistan"
    4: "Training and Development Officer in Pakistan"


    remember these are the samples

    What I would like to get is the word "in" in the major string
    If I use instr or instrRev functions they pic in that is in "training"
    What my logic is (You may have your own which I would like to know also) if the character on the right other than the substring and left of that substring other than the substring is non alpha then that is my word.else not

    like ain ,in is not myword as it has alpha character on the left
    like inin ,in is also not my word as it also have a alpha on the right
    but " in-" , here in is my word

    How can I pass the main string from this logic in VBA

    Help would be greatly appreciated

    Best Regards
    Imran Bhatti

    Hope that makes sense.
    Imran Bhatti
    Last edited by ImranBhatti; 12-14-2017 at 08:08 AM.
    Teach me Excel VBA

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    What do you mean by "get"? Do you just need to check if it is present, or do you need its position?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2021
    Posts
    5,941

    Re: How to get substring in a major string with VBA

    Try:

    If InStr(Replace(" " & str & " ", "-", " "), " in ") > 0 Then

  4. #4
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Just determine if it exists

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    In addition to Phuocam's solution above, you could also use regex
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Hi both the legend

    I am signing of from my office at the moment. Its time to leave .will test both the solutions at whome after 6 PM (my time today ) and let you know the outcome .

    Thanks for the review. bye for 2 hours.

    Imra bhatti

  7. #7
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Quote Originally Posted by Phuocam View Post
    Try:

    If InStr(Replace(" " & str & " ", "-", " "), " in ") > 0 Then
    Tried but not working . Am I missing something
    Please Login or Register  to view this content.
    Last edited by ImranBhatti; 12-14-2017 at 11:04 AM. Reason: Code correction

  8. #8
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Quote Originally Posted by xlnitwit View Post
    In addition to Phuocam's solution above, you could also use regex
    How can I apply this Function ContainsIn?

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    I don't think anything is going to work if your string is actually "TraininginPakistan" since there is nothing to delimit what is the word and what isn't.

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Tried this (s contains my required separated by / and - these two characters are non alpha so it should return true.but not .Here I think your function can help. I am currently not familiar with regex.
    Please Login or Register  to view this content.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    With my function you would simply use
    Please Login or Register  to view this content.
    but you will also need to change your declarations to make s a String not a Variant
    Please Login or Register  to view this content.
    Last edited by xlnitwit; 12-14-2017 at 11:13 AM.

  12. #12
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Please Login or Register  to view this content.

    ByRef argument type mismatch. Error

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    I guess you didn't see the edit to my post- you have declared s as a Variant and it needs to be a String.

  14. #14
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Changed s to word in brackets

    Now tested this and returns true but it is not true

    TraininginPakistan"
    Now see your edit

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    It shouldn't (and doesn't for me). What is the actual code you are using now?

  16. #16
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Applied your edit but still problem. Do I also need to change ..Pattern = "\bin\b" to something else?

  17. #17
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Actual code

    Please Login or Register  to view this content.

  18. #18
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    in may be something else as well

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    That's not correct- it should be
    Please Login or Register  to view this content.
    not
    Please Login or Register  to view this content.

  20. #20
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    Quote Originally Posted by ImranBhatti View Post
    in may be something else as well
    Might have been a good idea to start the thread with that! You could add that as a parameter
    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    Thanks for the correction.Problem is 99.99 % solved . It seemed that the under score "_" has an exceptions of regex. with this expression it returns false while it is true.

  22. #22
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    There may well be a simpler solution but I think this should suffice
    Please Login or Register  to view this content.

  23. #23
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    And What should I pass ContainsIn(Here?) for the new function according to my actual code

  24. #24
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    The same as before- i.e.
    Please Login or Register  to view this content.

  25. #25
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: How to get substring in a major string with VBA

    That worked for underscore as well. Thank you so much Sir Don for your precious time to review my thread and for the long long discussion.I can see the system generated the second page of the thread.

    many thanks indeed for spreading the knowledge.

    Best Regards
    Imran Bhatti

  26. #26
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to get substring in a major string with VBA

    You're welcome.

+ 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. Need help in extracting substring from string
    By juixyy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-14-2016, 05:28 AM
  2. [SOLVED] Copy a substring from a string
    By raul09 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-02-2013, 12:53 AM
  3. Finding a substring within a string
    By Michael D in forum Excel General
    Replies: 7
    Last Post: 02-21-2013, 04:01 PM
  4. [SOLVED] extract from string whole word(s) containing substring
    By Bearpecs in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-24-2012, 10:37 AM
  5. Searching for a substring in a string
    By 1rovilla in forum Excel General
    Replies: 3
    Last Post: 05-07-2010, 08:17 PM
  6. Substring based on position in string
    By yellephant in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2007, 10:12 AM
  7. check substring of a string in Excel VBA
    By magix in forum Excel General
    Replies: 2
    Last Post: 12-11-2005, 11:10 AM

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