Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable used as condition

Status
Not open for further replies.

pepsi123

Programmer
Jul 30, 2009
5
US
Hi, i have this requirement where user enters the condition and VBA needs to check it in IF condition.
example.. user will enter "age > 45"

VBA code should check as below..
if age > 45 then
'show some messages
end if

how to accomplish this?

Thanks
 
I am trying to write this dynamic code in VBA.
 
Repeat: what application?

"where user enters the condition"

Does that mean this is code on a userform? If so, say so.

if age > 45 then
'show some messages
end if

age (as it is written) is a variable. Do you have it properly declared and given a value?
example.. user will enter "age > 45"
That sounds like the user is entering a string, which would not be the same as a variable age.

Please read the FAQ on how to ask good questions. It may help.

Essentially though, IF age is a variable, then:

if age > 45 then
'show some messages
end if

is exactly how you would accomplish it.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
I think the OP means something like this (pseudocode):



dim enteredText as string

' User would enter string here
enteredText = "age > 45"

if <<evaluate enteredText>> then
'show some messages
end if



I'm not sure this can be done. Would other variables would/could the user enter? Is it always going to be 'age', or could it be more/other items?

I think a userForm with popups for item (age, height, weight, or whatever)and evaluator (>, <, =, etc.), and a text field to enter the value would probably be your best bet.
 
It can be done. Whether it is worthwhile or not is another matter.

The following example assumes that you have set a reference to the Microsoft Script Control 1.0 and declared

Public myScript As New ScriptControl

Then the following example illustrates the basic solution:
Code:
[blue]    myScript.Language = "vbscript"
    myScript.AddCode "age=47"
    If myScript.Eval("age>45") Then
        MsgBox "It works!"
    Else
        MsgBox "Oh dear"
    End If[/blue]

We can get much more complex than this, but this is the basic idea.
 
wonderful strongM. I never knew abt this script control. Thank you. It is working.
 
I need help with the below problem in excel VBA.

My excel file Sheet1 has following columns with data as below.emprate is blank.

empname emptitle emprate
name1 Staff
name2 paralegal
name3 Staff


In Sheet2 of the same excel file user will enter as below.

Rule Rate
Title="Associate" 100
Title="Staff" 200
Title="paralegal" 300

I need to fill in the Sheet1.emprate column based on the rules from Sheet2.

How to use Microsoft script control for this?

Thank You
 



Get rid of
Title=
&
"

Then its a vanilla lookup.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi SkipVought,
Do you mean the Rule should be as below? without quotes?
Title=Associate

Actually user can set a rule based on title or YearJoined or grade of the employee.

The rule can vary different ways.

rule rate
title="associate" and grade=4 100
title ="associate" and yearjoined=2005 200
title="staff" or yearjoined >2000 300

can we read this with script control?
 



Check out Filter by using advanced criteria in Excel help. It seem that is similar to what you are trying to do.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top