Reputation: 309
I am working on summing up the values of multiple textboxes (A,B,C) into a single textbox (D) and also getting a percentage contribution of the textboxes (A/D , B/D, C/D) and placing the percentage values into other textboxes (E,F,G).
I was succesful in summing up the values using the following code
If A.Value = "" Then Exit Sub
If B.Value = "" Then Exit Sub
If C.Value = "" Then Exit Sub
D.Value = CDbl(A.Value) + CDbl(B.Value) + CDbl(C.Value)
I am facing difficulty in calculating the percentages of A, B, C into E,F,G boxes.
Would really appreciate if anyone can help me code this stuff out.
Thanks
Upvotes: 1
Views: 308
Reputation: 754
First and foremost, I think you should be using the .Text property of the texboxes if you want to set a new value for them for the user to see it.
There is a slight difference between .Text and .Value properties, that you can look up on the links I provided. To summarize, .Text displays the information, and can be different than the .Value held by the textbox.
What I understand according to what you wrote, is you want to display information for the user based upon input numbers.
Thus, I think the following code should do the trick:
' Also, to prevent the updating of every items of the Userfrom
' and then calculate the percentages, I would suggest using variables
Dim aVal As Single, bVal As Single, cVal As Single, tempVal As Single
If A.Text = "" Then Exit Sub
aVal = CDbl(A.Text)
If B.Text = "" Then Exit Sub
bVal = CDbl(B.Text)
If C.Text = "" Then Exit Sub
cVal = CDbl(C.Text)
tempVal = aVal + bVal + cVal
' Displaying the values in the different textboxes and rounding the percentages
D.Text = tempVal
E.Text = round(aVal / tempVal * 100, 2)
F.Text = round(bVal / tempVal * 100, 2)
G.Text = round(cVal / tempVal * 100, 2)
Regards.
Upvotes: 1