Consulting

Results 1 to 6 of 6

Thread: EVal function in Excel VBA

  1. #1

    EVal function in Excel VBA

    Hi
    I want to know if Eval function available in Excel VBA?

    I want to evaluate values like below
    st= "0>=15 or 0>=10"


    Thanks

  2. #2
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Check out the Evaluate function.

    What are you expecting your example to evalute to? As it stands it won't work.
    Glen

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why do you need a function?

    What exactly should that evaluate to?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub eval()
    Dim st As Boolean

    st = 0 >= 15 Or 0 >= 10
    MsgBox st

    st = 20 >= 15 Or 0 >= 10
    MsgBox st

    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Hi
    Actualy the expression is not a direct value. I am building the expression by checking various conditions and create the expression as string value.

    for e.g, after various validations, my expression will be like below

    ep="0>15 or 0>10"

    now I want to execute the above expression,

    if (ep)
    {
    do something
    }
    else
    {
    do otherthing
    }

    since ep is a string, it gives error in "if". for this scenario VBScript has Eval function which will evaluate the string expression and return the value.
    But VBA doesn't have Eval function.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub eval()
    Dim st As String

    st = "=or(0 >= 15, 0 >= 10)"
    MsgBox Evaluate(st)

    st = "=or(0 >= 15, 20 >= 10)"
    MsgBox Evaluate(st)
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •