+ Reply to Thread
Results 1 to 10 of 10

If statements in VBA to look at multiple criteria without a nested formula

Hybrid View

PFDave If statements in VBA to look... 05-23-2016, 09:35 AM
Keebellah Re: If statements in VBA to... 05-23-2016, 09:57 AM
PFDave Re: If statements in VBA to... 05-23-2016, 10:05 AM
Marc L Re: If statements in VBA to... 05-23-2016, 10:34 AM
Keebellah Re: If statements in VBA to... 05-23-2016, 10:51 AM
PFDave Re: If statements in VBA to... 05-23-2016, 10:57 AM
Keebellah Re: If statements in VBA to... 05-23-2016, 11:05 AM
Keebellah Re: If statements in VBA to... 05-23-2016, 10:41 AM
PFDave Re: If statements in VBA to... 05-23-2016, 10:52 AM
Keebellah Re: If statements in VBA to... 05-23-2016, 11:04 AM
  1. #1
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    If statements in VBA to look at multiple criteria without a nested formula

    Hi,

    I would like to write in VBA a list to advise IF contains this then return this for a number of outcomes. I have seen in the past this used to look for postcodes and then using the formula bar you can reference the code to check a cell rather than nesting IF statements.

    Basically I want to check if cell contains APP then return APP, Else IF cell contains CARD then return CARD etc etc.

    In excel I'd then just input

    =(subname(CELL))

    Thanks

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: If statements in VBA to look at multiple criteria without a nested formula

    Nice try, but you explanation doesn't paint a picture for me.
    I think what you are looking for a user defined function (UDF) but ... assumptions always take you the wrong way.
    Why not upload a simple file with some dummy data and show in it what you have and what you want to achieve?
    Version of Excel?
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: If statements in VBA to look at multiple criteria without a nested formula

    Ok here's a sample,

    Thanks
    Attached Files Attached Files

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

    Re: If statements in VBA to look at multiple criteria without a nested formula


    Hi !

    You can place your arguments list in a cells range and combine IFERROR and MATCH function for example …

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: If statements in VBA to look at multiple criteria without a nested formula

    If the text yiu want is always in the middel I would do it like this:

    Public Function getTag(myString As String) As String
    Dim fstring As Variant
    fstring = Split(myString, " ")
    getTag = fstring(LBound(fstring) + 1)
    End Function
    Copy the function into a vba module

    then in cell
    D2=getTag(B2)
    and you get Dog

    There is no test to check B2 is empty or not, just quick-and-dirty, split the text and take the text in the center.
    If the text is always the second word, it will work with longer texts too

    If not then you will always be stuck with one or more if then else
    Give it a try and let me know

  6. #6
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: If statements in VBA to look at multiple criteria without a nested formula

    Quote Originally Posted by Keebellah View Post
    If the text yiu want is always in the middel I would do it like this:

    Public Function getTag(myString As String) As String
    Dim fstring As Variant
    fstring = Split(myString, " ")
    getTag = fstring(LBound(fstring) + 1)
    End Function
    Copy the function into a vba module

    then in cell
    D2=getTag(B2)
    and you get Dog

    There is no test to check B2 is empty or not, just quick-and-dirty, split the text and take the text in the center.
    If the text is always the second word, it will work with longer texts too

    If not then you will always be stuck with one or more if then else
    Give it a try and let me know
    Keebellah, I'm sorry but the text could be anywhere and the LEN functions advises the text could be anywhere in up to 1007 characters.They are not also split by spaces and the text may be within other characters. I also need the return value to be specifically written as while it will start with IF dog then DOG, we created incidents for these and DOG would then refer to incident xxx so I need to be able to configure that as I go.

    This function then would be housed in numerous documents but I will update the VBA code only once to update.

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: If statements in VBA to look at multiple criteria without a nested formula

    If you want to use for for more than one file I'd advice to create an Addin so it can be adreesed/referenced without having to copy vba code to every file

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: If statements in VBA to look at multiple criteria without a nested formula

    I'll check it out and get back to you.

  9. #9
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: If statements in VBA to look at multiple criteria without a nested formula

    Thanks Keebellah.

    Marc L, I'm aware there are a few ways to do this using existing formulas however due to the expanding and complex nature of my source data I need to house it in VBA

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: If statements in VBA to look at multiple criteria without a nested formula

    Then you will have to have a list of possible values in a worksheet and give this range a name
    The macro will then have to check the contents of the long text against one of the values and if found display it.

    what happens if Dogcoockies have been fed to the cat.
    Dog is the first word so Cat will not be seen.

+ 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. [SOLVED] Excel formula nested functions max, if, and multiple criteria issues
    By Jack7774 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-19-2016, 04:38 PM
  2. multiple criteria nested lookup formula - only the strong need apply
    By boredombulker in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-11-2015, 03:44 PM
  3. Formula return statements on multiple criteria
    By welshman010 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-21-2014, 08:34 PM
  4. [SOLVED] Creating an IF/ELSE formula with multiple criteria - Nested IF/ELSE?
    By Doofus1 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-21-2014, 08:34 PM
  5. [SOLVED] Nested if statements containing multiple and statements
    By John M. in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-22-2014, 11:48 PM
  6. Multiple nested IF statements and AND statements
    By TonyGetz in forum Excel General
    Replies: 2
    Last Post: 12-14-2010, 03:07 AM
  7. Replies: 12
    Last Post: 05-15-2009, 08:38 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