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:
You (probably) do not want to sum the values from your textboxes when someone tries to change the value in the totals box - you want to sum them when a value in any of the other eight boxes change.
The simplest way to do this is probably to create a simple macro to sum the values of the textboxes, and then call that from the change-events of the 8 which the user may change.
The macro to sum the textboxes and put them into the totals box
Private Sub sum_boxes()
txtTotal = CLng(txtKas.Value) + CLng(Investasi.Value) + CLng(DanaTerbatas.Value)
End Sub
What a change event will look like
Private Sub txtKas_Change()
Call sum_boxes
End Sub
You need to make one change-event for each of the eight boxes, as mentioned previously.
On a completely different note, using a textbox to store the total may be a bad idea, as you don't want your users to change what's in it. One option is to lock the textbox, as PatricK suggests, another is to use a different object to hold the number, e.g. a label, and just change its caption to be similar to whatever your total is at the moment.