+ Reply to Thread
Results 1 to 18 of 18

How to check if a string contains one of the patterns using And & Or in If statement

  1. #1
    Registered User
    Join Date
    08-07-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    95

    How to check if a string contains one of the patterns using And & Or in If statement

    Hi there,

    I want to check a cell. If it doesn't meet one of the patterns below, it will return error msgbox.

    1. Start with "OP" and the length is 8, or
    2. Start with "DF" and the length is 10

    I wrote a code like this:

    Please Login or Register  to view this content.
    However, it doesn't work. Could you please point out why?

    Thank you so much,
    Harry
    Last edited by qzqzjcjp; 09-03-2019 at 08:37 AM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: And Or

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however the thread title does not really convey what your request is about.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    Obviously :

    If Not ((Cell.Text Like "OP*" And Len(Cell.Text) = 8) Or (Cell.Text Like "DF*" And Len(Cell.Text) = 10)) Then

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

    Re: Hi ! Try this !

    Quote Originally Posted by Marc L View Post

    Obviously :

    If Not ((Cell.Text Like "OP*" And Len(Cell.Text) = 8) Or (Cell.Text Like "DF*" And Len(Cell.Text) = 10)) Then
    Another way...

    If Not (Cell.Value Like "OP??????" Or Cell.Value Like "DF????????") Then

  5. #5
    Registered User
    Join Date
    08-07-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    95

    Re: Hi ! Try this !

    Quote Originally Posted by Marc L View Post

    Obviously :

    If Not ((Cell.Text Like "OP*" And Len(Cell.Text) = 8) Or (Cell.Text Like "DF*" And Len(Cell.Text) = 10)) Then
    Thank you very much!!!

  6. #6
    Registered User
    Join Date
    08-07-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    95

    Re: Hi ! Try this !

    Quote Originally Posted by Rick Rothstein View Post
    Another way...

    If Not (Cell.Value Like "OP??????" Or Cell.Value Like "DF????????") Then
    Thank you so much! I am now using it.

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

    Re: Hi ! Try this !

    Quote Originally Posted by qzqzjcjp View Post
    Thank you so much! I am now using it.
    You are quite welcome. When I responded, I did so directly to Marc's posting and did not pay attention to that there was a message before it from an Administrator advising you that your thread title needed "repair". Theoretically, neither Marc nor I should have responded until you complied with the Administrator's request. You got away with it this time, although it would be nice if you fixed the thread title so people doing searches in the future might locate this thread easier when trying to find a solution to their own similar problems. Anyway, you should read Message #2 and comply with it... both now would be nice and in future postings for sure.

  8. #8
    Registered User
    Join Date
    08-07-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    95

    Re: Hi ! Try this !

    Quote Originally Posted by Rick Rothstein View Post
    You are quite welcome. When I responded, I did so directly to Marc's posting and did not pay attention to that there was a message before it from an Administrator advising you that your thread title needed "repair". Theoretically, neither Marc nor I should have responded until you complied with the Administrator's request. You got away with it this time, although it would be nice if you fixed the thread title so people doing searches in the future might locate this thread easier when trying to find a solution to their own similar problems. Anyway, you should read Message #2 and comply with it... both now would be nice and in future postings for sure.
    Thank you again Rick for pointing out. I have already fixed the title. Hope it meets the requirement.

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: How to use And Or in If statement

    qzqzjcjp Rick is correct, you need to correct your title to something that reflects your problem, not what you thing the answer should be. Honestly, "How to use And Or in If statement" is hardly more descriptive of your problem than "And Or" is.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by Rick Rothstein View Post
    Another way...

    If Not (Cell.Value Like "OP??????" Or Cell.Value Like "DF????????") Then
    Rick, obviously so obvious ! (I can't understand why the forum does not want I raise your reputation …)

    At least the OP should know where his error was …
    Last edited by Marc L; 09-03-2019 at 02:49 AM.

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

    Re: How to use And Or in If statement

    Quote Originally Posted by Marc L View Post
    Rick, (I can't understand why the forum does not want I raise your reputation …)
    Really? You tried to add to my reputation and was not allowed to? Curious... I wonder why?

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Message : « You must spread some Reputation around before giving it to Rick Rothstein again. »

    Even if it was days ago …

  13. #13
    Registered User
    Join Date
    08-07-2014
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    95

    Re: How to use And Or in If statement

    Quote Originally Posted by FDibbins View Post
    qzqzjcjp Rick is correct, you need to correct your title to something that reflects your problem, not what you thing the answer should be. Honestly, "How to use And Or in If statement" is hardly more descriptive of your problem than "And Or" is.
    Sorry about it. I changed it again. Hope it can work...

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

    Re: How to check if a string contains one of the patterns using And & Or in If statement

    Quote Originally Posted by Marc L View Post
    Message : « You must spread some Reputation around before giving it to Rick Rothstein again. »

    Even if it was days ago …
    Yes, it was several days ago... 13 to be exact (Aug. 21st). That does not seem like a very fair restriction to place on the Reputation Point system. Just because you did not find any postings to be worthy of giving them Reputation Points since you last gave some to me, I cannot get a boost to my reputation no matter how worthy you felt my posting was. Let's think of it in a different way. What if I were to become very active in this forum and posted two mind-blowing solutions (not that I am saying my solutions are ever mind-blowing... this is just a hypothetical after all:wink to two different questions on the same day... each of the regulars here would only be able to honor one of them with Reputation Points unless they gave someone else Reputation Points in the interim. I really do not understand that kind of restriction.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Yes I have some issues in this forum and after a workaround - one more here - I just raise your reputation !

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

    Re: How to check if a string contains one of the patterns using And & Or in If statement

    Quote Originally Posted by Marc L View Post

    Yes I have some issues in this forum and after a workaround - one more here - I just raise your reputation !
    Thank you... much appreciated.

  17. #17
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: How to check if a string contains one of the patterns using And & Or in If statement

    I knew there was a restriction placed (a while back we had a pair of members repping each other, back and forth, 5 times a day, daily...to the tune of 1000's of re points).

    But I did not think there was that much of a limitation. I will "TRY" to check into it with the tech team, but don't hold your breath waiting.

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

    Re: How to check if a string contains one of the patterns using And & Or in If statement

    Quote Originally Posted by FDibbins View Post
    I knew there was a restriction placed (a while back we had a pair of members repping each other, back and forth, 5 times a day, daily...to the tune of 1000's of re points).

    But I did not think there was that much of a limitation. I will "TRY" to check into it with the tech team, but don't hold your breath waiting.
    Thank you in advance for checking into this... appreciated.

+ 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