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 (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
What a change event will look like
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.
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):
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:
Now back to the UserForm1 object, right click TextBox1 and View Code, put in:
Also do checking on the KeyPress event:
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: