• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Converting a string having an expression like a > b to boolean

Hello,

in vb 6 can I get a Boolean value like true or false from a string which contains (a-b) where a and both are numbers.

Please help

thanks in advance
 
Sourav

Firstly, Welcome to the Chandoo.org Forums

Boolean's come from comparison not arithmetic operations

eg: =a=b
if both a and b have the same value then the answer is True, otherwise it is False

If the values a and b are in a string then they will need to be extracted unless you are comparing strings

eg: =a=b will be true if both a and b = "Cat" but False if one doesn't equal "Cat"

Can you provide more data as to what you actually require, even a sample file is useful
 
First of all,thank you for the reply,

However my problem was

dim str as string

dim a as integer

dim b as integer

a=15

b=10

str="a-b"

Now I want the str to be Boolean value like true or false like if i can cbool(str) the true value returns

thanks for the welcome

Sourav
 
Hi Sourav ,

You can make use of the Application.Evaluate function ; however , what this evaluates are strings which resolve to valid expressions.

A string expression such as :

str1 = "a = b"

is not an expression , but just a text string.

Note : I have used str1 and not str , since Str is a VBA keyword representing a function. Never use keywords as variable names.

In order for the Application.Evaluate to correctly evaluate the expression that you want evaluated , try this :

str1 = "=" & a & ">" & b

What will be done here is that the values contained in the variables a and b will be inserted in the string , so that what the Application.Evaluate function gets will be the following :

str1 = "=10>5"

assuming that the variable a has been assigned the value 10 , and the variable b has been assigned the value 5.

Thus , the following :

?Application.Evaluate(str1)

entered in the Immediate window , will get the result True.

Narayan
 
Back
Top