如何将8个文本框中的值归结为单个文本框? [英] How can I sum value in 8 textboxes to the a single textbox?

查看:207
本文介绍了如何将8个文本框中的值归结为单个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到这样做的方法。我认为这是一个非常基本的问题,但是我只是崭新的excel VBA,找不到正确的公式。



最近我明白,要做的总和,我必须更改每个 文本框 strong>到整数,然后我可以将它添加到单个 文本框 中,所以我将每个值转换为整数,而我总计一个一个到一个文本框。



我已经尝试过这个代码。

  Private Sub txtTotal_Change()
txtTotal.Value = CInt(txtKas)+ CInt(txtInvestasi)+ CInt(txtDanaTerbatas)+ CInt(txtBruto)+ ...
End Sub

我如何将这个多个文本框(8个文本框)合并到一个文本框中?

解决方案

您需要在用户可更改的文本框中添加更改事件。 TextBox1 TextBox3



TextBox3将为TextBox1和TextBox2的总和。



右键单击项目下的 UserForm1 ,然后选择查看代码,然后放入 TextBoxesSum Sub(我使用Double类型接受小数):

  Private Sub TextBoxesSum()
Dim Total As Double
总计= 0
如果Len (TextBox1.Value)> 0 Then Total = Total + CDbl(TextBox1.Value)
如果Len(TextBox2.Value)> 0 Then Total = Total + CDbl(TextBox2.Value)
'为其余的文本框添加更多
TextBox3.Value =总
End Sub

为了安全和智能,您还应该从用户输入进行密钥检查。由于文本框的数量,你最好有一个函数来处理它:

 私有函数NumericOnly(ByVal KeyAscii作为MSForms.ReturnInteger)作为MSForms.ReturnInteger 
Dim键作为MSForms.ReturnInteger
选择案例KeyAscii
案例46,48到57'只接受十进制。和数字[0-9]
设置键= KeyAscii
案例Else
KeyAscii = 0'较小的错误早期
设置键= KeyAscii
结束选择
设置NumericOnly = Key
结束函数

现在返回到UserForm1对象,右键单击TextBox1和查看代码,放入:

  Private Sub TextBox1_Change()
TextBoxesSum
End Sub

还要检查KeyPress事件:

  Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = NumericOnly(KeyAscii)
End Sub

现在复制上面的2套TextBox Sub,然后粘贴以下,然后在下划线之前替换字符串,以匹配您拥有的文本框的名称。 / p>

示例输出:


I can't find a way to do this. I think it's a very basic question, but i'm just new with excel VBA and can't find the right formula.

Recently i understand that to do the sum, i have to change each of the textboxes into integer first, then i can add it to a single textbox, so i'm converting each value into integer while i sum them one by one to a text box.

I have tried this code.

Private Sub txtTotal_Change() 
txtTotal.Value = CInt(txtKas) + CInt(txtInvestasi) + CInt(txtDanaTerbatas) + CInt(txtBruto) + ...
End Sub

How can i sum this multiple textboxes (8 textboxes) into a single textbox?

解决方案

You need to add Change Events on your text boxes that are user changeable.

Lets say I have below UserForm and TextBox1 to TextBox3:

TextBox3 will be the Sum of TextBox1 and TextBox2.

Right click UserForm1 under Project and select View Code, then put in the TextBoxesSum Sub (I use Double type for accepting decimals):

Private Sub TextBoxesSum()
    Dim Total As Double
    Total = 0
    If Len(TextBox1.Value) > 0 Then Total = Total + CDbl(TextBox1.Value)
    If Len(TextBox2.Value) > 0 Then Total = Total + CDbl(TextBox2.Value)
    ' Add more for the rest of your text boxes
    TextBox3.Value = Total
End Sub

To be safe and smart, you should also put in key checking from user input. Due to the amount of text boxes you have it is better to have a Function to handle it:

Private Function NumericOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger
    Dim Key As MSForms.ReturnInteger
    Select Case KeyAscii
        Case 46, 48 To 57 ' Accept only decimal "." and numbers [0-9]
            Set Key = KeyAscii
        Case Else
            KeyAscii = 0 ' Minor bug earlier
            Set Key = KeyAscii
    End Select
    Set NumericOnly = Key
End Function

Now back to the UserForm1 object, right click TextBox1 and View Code, put in:

Private Sub TextBox1_Change()
    TextBoxesSum
End Sub

Also do checking on the KeyPress event:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = NumericOnly(KeyAscii)
End Sub

Now copy above 2 set of TextBox Sub and paste below, then replace the string before underscore to match the name of the text boxes you have.

Sample output:

这篇关于如何将8个文本框中的值归结为单个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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