+ Reply to Thread
Results 1 to 12 of 12

Creating an If statement with a variable operator

  1. #1
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Creating an If statement with a variable operator

    I am not sure if this is possible, but can you create a dynamic if statement, where the operator is a variable?
    I want to create a dropdown menu where the user can select an operator (=,<>,>,etc), and a macro will perform a validation based off of that selection.
    Please let me know if you require any further details, or have any ideas.

    Ex:
    instead of something like:
    If Variable1 = "Test" then...

    I would like it to be something like:
    If Variable1 VariableOperator1 "Test" then...

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Creating an If statement with a variable operator

    Not sure if this will do what you need, but it might work. This assumes the user selected operator is in cell A1

    Please Login or Register  to view this content.
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  3. #3
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: Creating an If statement with a variable operator

    Hi,

    EVALUATE is a function which can convert formula in string format to real function.
    Try using something like:

    Please Login or Register  to view this content.
    Here say A1 has a value A2 is operator (>,< etc) and A3 is a value.

  4. #4
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Creating an If statement with a variable operator

    Hi Speshul and misrasomendra,
    Thank you for your responses. I think that the Evaluate function is the direction I am looking towards. I will play around with the function and see if I can get it to work for my purposes. Cheers!

  5. #5
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: Creating an If statement with a variable operator

    You can also use EVALUATE function in name manager through named formula, if the requirement is not much further in to macro. But then also you have to save the file as MACRO enabled .xlsm format.

    If you feel the solution will help you than you can give reputation by clicking the reputation star below my comment

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Creating an If statement with a variable operator

    Hello Rshwar23,

    I find you request too wide to pinpoint it to an exact requirement.

    Attach a sample workbook.

    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  7. #7
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Creating an If statement with a variable operator

    Hi Winon,
    I actually think it will be less confusing if I show you what I intend to do, as the macro is quite large.
    I want to take a text string similar to this:
    {|Supplier|=|Pro|AND|Worker|=|Ryan|AND|Bill Rate|>|20|AND|Mark Up|=>|25|AND|OT Multiplier|=|25|}
    And extract the individual data elements (I already have code for this).
    Ex
    If Supplier = Pro AND worker = Ryan ...

    and then use these elements to validate data against. So, they would need to be put into some sort of IF statement. I think that the EVALUATE function might be able to be manipulated to make this work.
    Each of those data elements can have between 5 and 30 choices in a dropdown menu, so a simple if statement doesn't make sense.

    I hope I explained this well, but if you still would like to see a spreadsheet, I can make a sample for you.
    Thanks in advance for any advice.

    Quote Originally Posted by Winon View Post
    Hello Rshwar23,

    I find you request too wide to pinpoint it to an exact requirement.

    Attach a sample workbook.

    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    Regards.

  8. #8
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Creating an If statement with a variable operator

    Quote Originally Posted by misrasomendra View Post
    Hi,

    EVALUATE is a function which can convert formula in string format to real function.
    Try using something like:

    Please Login or Register  to view this content.
    Here say A1 has a value A2 is operator (>,< etc) and A3 is a value.
    Hi misrasomendra,
    I tested it out, and it doesn't appear to do quite what I want. Any ideas?


    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: Creating an If statement with a variable operator

    Try below code:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Creating an If statement with a variable operator

    Hi
    misrasomendra,
    That worked! Thank you. Just so I understand what the formula did, the """" just encloses each variable in quotation marks? So Excel would read it as Evaluate( "Pro" "=" "Pro" ) ?

  11. #11
    Valued Forum Contributor
    Join Date
    04-09-2013
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    391

    Re: Creating an If statement with a variable operator

    Yes other wise the string would have been "Pro=Pro" so type mismatch.

  12. #12
    Registered User
    Join Date
    06-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Creating an If statement with a variable operator

    Thank you again for your help. This will help me out quite a lot.

+ 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. Using LIKE Operator in VBA Variable
    By ragonef in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-12-2016, 02:22 PM
  2. [SOLVED] Creating new Arithmetic Operator
    By RGrunden in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-23-2012, 01:30 PM
  3. format within an & operator embedded in an IF statement
    By carsto in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-04-2006, 12:03 PM
  4. Combining Nested IF statement with Between operator
    By Daisy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-13-2005, 05:30 PM
  5. [SOLVED] How dio I attach a variable to an Operator:= ?
    By Richard Buttrey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2005, 06:05 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