检查文本框文本是Excel有效公式,并使用该功能在VBA中计算,而无需在Excel表中存储任何值
我正在尝试学习Excel VBA。我坚持的任务之一是如何验证textbox1.text
是有效的Excel公式。我尝试了worksheetfunction.infunction(userform1。text1.text)
,但它给出了错误。我的目标是验证userform1.text1.text =有效
excel函数(例如x^2或sin(x))。如果有效,请使用评估
函数/方法来计算值。尝试的文本如下:
if worksheetfunction.isformula(userform1.text1.text) = true then A(i).value = worksheetfuntion.evaluate(userform1.text1.text,"x", B(i))
a和b是值数组作为变量 我无法理解错误是什么,有帮助吗?
I am trying to learn EXCEL VBA. One of the tasks I am stuck with is how to verify textbox1.text
is valid excel formula. I tried Worksheetfunction.isfunction(userform1. text1.text)
but it is giving error. My objective is to validate the userform1.text1.text = valid
EXCEL function (say x^2 or sin(x)). If valid then use Evaluate
function/method to calculate the value. Tried text as follows :
if worksheetfunction.isformula(userform1.text1.text) = true then A(i).value = worksheetfuntion.evaluate(userform1.text1.text,"x", B(i))
A and B are value arrays as varient
I am not able to understand what the error is, any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
a)
worksheetfunction.isformula
期望一个范围为参数 - 换句话说,您可以检查单元是否包含公式,但不是字符串是公式。如果您尝试使用类型不匹配错误(因为字符串和范围不是兼容类型)。b)WorksheetFunction没有
评估
-Method,您需要使用application.evaluate
(您可以省略application
)c) c)
评估>评估
如果无法评估您通过的字符串(因为它包含无效公式),则会导致错误(2015年错误)。您可以检查函数iserror
的结果是否。但是,请注意,在某些情况下,公式本身有效,但结果是错误(例如,您的公式为“ a1/a2”
,而单元格A2包含0,您会得到A 通过0 错误分割)。d)
评估
将在有没有领先的=
的情况下评估字符串,而Excel中的公式始终以=
开始。a)
worksheetfunction.isformula
expects a Range as parameter - with other words, you can check if a cell contains a formula, but not if a string is a formula. You get a type mismatch error if you try (because a string and a range are not compatible types).b) WorksheetFunction has no
evaluate
-method, you need to useApplication.Evaluate
(you can omit theApplication
)c)
evaluate
result in an error (Error 2015) if the string you pass cannot be evaluated (because it contains an invalid formula). You can check if the result with the functionIsError
. However, be aware that there might be cases where the formula itself is valid, but the result is an error (eg your formula is"A1/A2"
and cell A2 contains 0, you get a Division by 0 error).d)
evaluate
will evaluate the string with and without leading=
while a formula in Excel always starts with the=
.