+ Reply to Thread
Results 1 to 10 of 10

How to program to account for all choices "ABC*", "*ABC", "*ABC*"

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    Hi,

    I have a situation where i an array of strings. I am supposed to allow the user to select one of three options
    ABC*
    *ABC
    *ABC*

    Any suggestions on how to program it so i account for all 3 or do i have to have multiple if statements?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    Do all options have the same outcome?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    Quote Originally Posted by welchs101 View Post
    I have a situation where i an array of strings. I am supposed to allow the user to select one of three options
    ABC*
    *ABC
    *ABC*
    Any suggestions on how to program it so i account for all 3 or do i have to have multiple if statements?
    In VBA, you might use:

    Please Login or Register  to view this content.
    where "s" is a String or Variant variable. Of course, you might use s(i) for a single string element in a one-dimensional array. (Insufficient information for us to provide a turn-key answer.)

    But beware: that only recognizes all-uppercase instances of "ABC". If you want to allow for lowercase as well as uppercase, try:

    If InStr(UCase(s),"ABC") > 0 Then

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    Maybe something like this...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    i responded yesterday but i guess i got disconnected and it never got posted.

    So here is the situation.

    Sometimes the user wants to
    1-Have starts with "ABC"
    2-Ends with "ABC"
    3-Contains "ABC"

    Now depending on which it is i will analyze the string for this scenario. The only way i know to do it now is to have 3 if/then statements one if its starts with , one if its ends with and one if its contains.......

    was wondering if there was a slick way to only have a single if/then

    any ideas?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    Use this syntax for IF
    If Then
    ElseIf Then
    ElseIf Then
    End If


    Or use the Select Case statement as in the example code above which does the same thing.
    Last edited by AlphaFrog; 05-19-2014 at 06:21 PM.

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    ok......i dont think i explained this well but i think i found the answer.

    so lets suppose that the user can put one of the following in cell A1: "Site*", "*Site", "*Site*"

    Let the value of cell A1 equal the variable string1

    now lets suppose in cell B1 i put "abcSite"

    Let the value of cell B1 equal the variable string2

    now i want to compare string2 (cell B1 contents) to cell A1

    i can do the following


    Please Login or Register  to view this content.

    i dont need to do 3 if/then comparisons.......just one.

    make sense?

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    anyone see any issues with this method? better to ask and find out now vs program it and find out later.......

    thanks again for all your help and sorry for the confusion

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    It should be fine - just remember that Like is also case sensitive so if you don't want that, you shoudl use LCase$ (or UCase$) on both variables.
    Remember what the dormouse said
    Feed your head

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: How to program to account for all choices "ABC*", "*ABC", "*ABC*"

    thanks.....I did not know that about "Like" so I am glad I asked......

    thanks to all who helped......I appreciate 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. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  2. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  3. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  4. Replies: 7
    Last Post: 05-13-2006, 05:02 PM
  5. Replies: 1
    Last Post: 01-30-2006, 06:10 PM

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