简体   繁体   English

当其中一个值为文本时,Excel VBA工作表sum函数返回错误

[英]Excel VBA worksheet sum function returns error when one of values is text

I get this error: "Run-time error '1004'"" Unable to get the Sum property of the Worksheetfunction class 我收到此错误:“运行时错误'1004'”“无法获取Worksheetfunction类的Sum属性

I have a code that looks into multiple comboboxes in a userform and sums their values that the user selects. 我有一个代码,用于查看用户表单中的多个组合框,并汇总用户选择的值。 The code was working fine with only numbers in the combobox list. 代码工作正常,只有组合框列表中的数字。 I recently added one text option into the list and I called it "Not applicable". 我最近在列表中添加了一个文本选项,我将其称为“不适用”。 However, once I run the code I get the above error. 但是,一旦我运行代码,我就会收到上述错误。

I believe the error is because I introduced the text option, and the sum function in VBA wouldn't recognize it. 我认为错误是因为我引入了文本选项,而VBA中的sum函数无法识别它。

Is there anyway to solve this issue? 反正有没有解决这个问题?

Thanks 谢谢

Here is the code I used: 这是我使用的代码:

Dim totalscore As Double
totalscore = WorksheetFunction.Sum(frmQA.ComboBox3.Value, frmQA.ComboBox4.Value, frmQA.ComboBox5.Value, frmQA.ComboBox6.Value, frmQA.ComboBox7.Value, frmQA.ComboBox8.Value, frmQA.ComboBox9.Value)

Assuming the text option is available in ComboBox3 adjust this part of the sum formula: 假设ComboBox3中的文本选项可用,请调整此部分的总和公式:

frmQA.ComboBox3.Value

To this: 对此:

Iif(IsNumeric(frmQA.ComboBox3.Value),frmQA.ComboBox3.Value,0)

If they all have the text option you can do this: 如果他们都有文本选项,您可以这样做:

totalscore = totalscore + Iif(IsNumeric(frmQA.ComboBox3.Value),frmQA.ComboBox3.Value,0)
totalscore = totalscore + Iif(IsNumeric(frmQA.ComboBox4.Value),frmQA.ComboBox4.Value,0)
...

It may be better to verify whether the entry made, by the user, is a text or numeric value before processing is done. 在处理完成之前,验证用户输入的条目是文本还是数值可能更好。

From a cursory glance at the code - I can surmise that the "text value" may go along the lines of '123 (see apostrophe mark).It's possible, but that would be a conscientious effort by the end user. 从粗略的一瞥代码 - 我可以推测“文本值”可能与'123(见撇号)一致。这是可能的,但这将是最终用户的尽职尽责。

So,therefore IMHO, it would be better to run a validation check before the calculation (as suggested by Scott Holtzman). 因此,恕我直言,最好在计算之前进行验证检查(如Scott Holtzman所建议的那样)。

"Not Applicable" - either use the number zero (0) in the code or somehow get your VBA to map "Not Applicable" and change this entry to zero(0) value. “不适用” - 使用代码中的数字零(0)或以某种方式让您的VBA映射“不适用”并将此条目更改为零(0)值。

Pseudo code example: 伪代码示例:

IF frmQA.ComboBox3.Value = "NOT Applicable" then

        frmQA.ComboBox3.Value = 0
END IF

The above suggestion can be placed in a worksheet event, perhaps 上面的建议也许可以放在工作表事件中

Best of luck 祝你好运

Regards 问候

RDFS RDFS

Here is the answer modified as suggested by Scott 这是斯科特建议修改的答案

Dim totalscore As Double
totalscore = WorksheetFunction.Sum(IIf(IsNumeric(frmQA.ComboBox3.Value), frmQA.ComboBox3.Value, 0), IIf(IsNumeric(frmQA.ComboBox4.Value), frmQA.ComboBox4.Value, 0), IIf(IsNumeric(frmQA.ComboBox5.Value), frmQA.ComboBox5.Value, 0), IIf(IsNumeric(frmQA.ComboBox6.Value), frmQA.ComboBox6.Value, 0), IIf(IsNumeric(frmQA.ComboBox7.Value), frmQA.ComboBox7.Value, 0), IIf(IsNumeric(frmQA.ComboBox8.Value), frmQA.ComboBox8.Value, 0), IIf(IsNumeric(frmQA.ComboBox9.Value), frmQA.ComboBox9.Value, 0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM