在Userform中不计算值 [英] Values not calculating in Userform

查看:217
本文介绍了在Userform中不计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我。



我有一个具有多个数字字段的用户窗体。问题是,我的合计框不会计算它们,因为它将一个数字识别为文本。所以例如,如果我要说HE.Value(10)+ ME.Value(10),例如should = 20然而,总计来到1010.如果我*它反而我收到一个错误调试,因为它将该值识别为文本格式。



我以前从未有过此类问题。有人能够协助吗?



谢谢,

解决方案

你有:

  totalBox.Text = box1.Text + box2.Text 

当两个操作数为 String 时, + operator作为一个字符串连接运算符,这意味着它的工作方式与& 运算符完全一样。这就是你如何获得 1010 而不是 20 :VBA认为你要求它连接字符串。



VBA不能对字符串执行算术,所以如果你的字符串代表数字值,那么你需要先转换它们。 >

请参阅数据类型和限制,以确定哪个是最合适的,但是如果您只使用整数,则可以使用 CLng 类型转换函数:

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

请注意,这使得返回字符串转换显式( CStr );离开它,VBA将隐式执行类型转换。


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,

解决方案

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.

See data types and limits to determine which is most appropriate, but 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.

这篇关于在Userform中不计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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