I'm in the process of learning VBA and am working on a League Manager for my local billiard hall (The guy likes Excel). I have created a UserForm to enter scores and I'm looking for an easier way to code up my controls in it. I have a TextBox that needs to display the sum of values contained in other TextBoxes in the UserForm. Instead of writing an AfterUpdate subroutine that contains the same code for each TextBox, is there a way to just say
-Update this sum if any of the TextBoxes change or are updated
My research has pointed me in the direction of using a class for TextBoxes but what I've read is that the WithEvents modifier doesn't support Change or Before/After Update and I haven't been able to find an example that solves my problem. Or rather an example that works for only the TextBoxes I want to group, not ALL TextBoxes. As mentioned, I can easily write a duplicate sub routine (like below) for each of the TextBoxes but I feel there is a more efficient way.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Private Sub txtPlayer1_1_Score6_AfterUpdate()
txtPlayer1_1_Total.Value = CInt(txtPlayer1_1_Score1.Value) + _
CInt(txtPlayer1_1_Score2.Value) + _
CInt(txtPlayer1_1_Score3.Value) + _
CInt(txtPlayer1_1_Score4.Value) + _
CInt(txtPlayer1_1_Score5.Value) + _
CInt(txtPlayer1_1_Score6.Value)
End Sub</code>Any help would be appreciated. Thank you.
-Update this sum if any of the TextBoxes change or are updated
My research has pointed me in the direction of using a class for TextBoxes but what I've read is that the WithEvents modifier doesn't support Change or Before/After Update and I haven't been able to find an example that solves my problem. Or rather an example that works for only the TextBoxes I want to group, not ALL TextBoxes. As mentioned, I can easily write a duplicate sub routine (like below) for each of the TextBoxes but I feel there is a more efficient way.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Private Sub txtPlayer1_1_Score6_AfterUpdate()
txtPlayer1_1_Total.Value = CInt(txtPlayer1_1_Score1.Value) + _
CInt(txtPlayer1_1_Score2.Value) + _
CInt(txtPlayer1_1_Score3.Value) + _
CInt(txtPlayer1_1_Score4.Value) + _
CInt(txtPlayer1_1_Score5.Value) + _
CInt(txtPlayer1_1_Score6.Value)
End Sub</code>Any help would be appreciated. Thank you.