Values not calculating in Userform

我与影子孤独终老i 提交于 2019-12-14 03:24:09

问题


Can someone please help me.

I have a userform which has a number of numeric fields. The issue is that my Total box will not calculate them as it is recognising a number as text. So for example if I was to say HE.Value (10) + ME.Value (10) for example should = 20 however is the Total is coming to 1010. and if I * it instead I am receiving an error to debug as it recognises the value as text format.

I have never had this issue in a userform before. Is someone able to assist please?

Thanks,


回答1:


You have:

totalBox.Text = box1.Text + box2.Text

When the two operands are String, the + operator works as a string concatenation operator, meaning it works exactly like the & operator. That's how you get 1010 instead of 20: VBA thinks you're asking it to concatenate strings.

VBA cannot do arithmetics on strings, so if your strings represent numeric values, you need to convert them first.

If you're only working with integers then you can do this with the CLng type conversion function:

totalBox.Text = CStr(CLng(box1.Text) + CLng(box2.Text))

Notice this makes the back-to-string conversion explicit (CStr); leave it out and VBA will perform that type conversion implicitly.



来源:https://stackoverflow.com/questions/44680791/values-not-calculating-in-userform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!