检查文本框文本是Excel有效公式,并使用该功能在VBA中计算,而无需在Excel表中存储任何值

发布于 2025-01-27 05:00:28 字数 461 浏览 3 评论 0原文

我正在尝试学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2025-02-03 05:00:29

a)worksheetfunction.isformula期望一个范围为参数 - 换句话说,您可以检查单元是否包含公式,但不是字符串是公式。如果您尝试使用类型不匹配错误(因为字符串和范围不是兼容类型)。

b)WorksheetFunction没有评估 -Method,您需要使用application.evaluate(您可以省略application

c) c)评估>评估如果无法评估您通过的字符串(因为它包含无效公式),则会导致错误(2015年错误)。您可以检查函数iserror的结果是否。但是,请注意,在某些情况下,公式本身有效,但结果是错误(例如,您的公式为“ a1/a2”,而单元格A2包含0,您会得到A 通过0 错误分割)。

d)评估将在有没有领先的=的情况下评估字符串,而Excel中的公式始终以=开始。

Dim formula as String, checkValue As Variant 
formula = userform1.text1.text
checkValue = Application.Evaluate(formula)
If not IsError(checkValue) Then 
    A(i).value = checkValue
Else
   MsgBox formula & " is not a valid formula"
End If

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 use Application.Evaluate (you can omit the Application)

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 function IsError. 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 =.

Dim formula as String, checkValue As Variant 
formula = userform1.text1.text
checkValue = Application.Evaluate(formula)
If not IsError(checkValue) Then 
    A(i).value = checkValue
Else
   MsgBox formula & " is not a valid formula"
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文