VBA - 将条件作为字符串进行评估 [英] VBA - Evaluate Conditions as String

查看:122
本文介绍了VBA - 将条件作为字符串进行评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的情况。在一个函数中,我将收到一系列待评估的条件。

I have come across a very strange scenario. In a function I will receive a string of condition to be evaluated.

例如

(a>b and (b=2 or c!=3))




其中a,b和c是我的变量名。

Where a,b and c are my variable names.

我试过搜索了很多但没有得到任何有意义的东西。

I tried and searched a lot but didn't get anything meaningful.

所以我的问题是:是否可以评估这样的字符串?如果是,请提供一些提示。

So my question is: Is it possible to evaluate a string like this? If yes, please provide me some hint on this.

推荐答案

替代方法,添加引用 Microsoft Script Control

Dim vx As MSScriptControl.ScriptControl
Set vx = New MSScriptControl.ScriptControl

a = 100
b = 200
c = 300
Cond = "(a>b and (b=2 or c<>3))"

With vx
    .Language = "VBScript"
    .AddCode "function stub(a,b,c): stub=" & Cond & ": end function"

    result = .Run("stub", a, b, c)
End With

MsgBox result

注意,您需要用<>替换!=,因为前者在VB *中无效(和/或是在jScript中无效)

Note you will need to replace != with <> as the former is not valid in VB* (and and/or is not valid in jScript)

这篇关于VBA - 将条件作为字符串进行评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆