I have a userform that adds multiple textboxes to a frame during runtime upon a user's selection from a listbox. The user will update the textboxes with values, and once complete, click a button on the userform that saves the textbox values to a worksheet, creates a label, and assigns the sum of the worksheet values from the textboxes to the label's caption. The first time the button is clicked everything works perfectly and the sum of textbox values is assigned to the label caption. When a new selection from the listbox is made, my code removes the label that was created from the button click, and also clears all of the textboxes. When new values are entered into the textboxes and the button is clicked a second time to create the total and labels, the labels are created but they still hold the values assigned from the first listbox selection. I don't know if this makes any sense, but ideally I need a way to completely clear out the value assigned to the label caption every time the button is clicked so that it updates the sum of the textboxes once they are updated after a second, third, etc... listbox selection. I have also tried adding the label to the form in the design mode and just updating the caption during runtime but have the same issue. My code so far looks like this and works only on the first listbox selection only...
Button Click Code To create label and assign sum of values on worksheet to label's caption:
Dim CstTotal1 As Object
Set CstTotal1 = UserForm2.ProjectInfo.Add("Forms.Label.1", "CstTotal1", True)
With CstTotal1
.Caption = Format(Application.WorksheetFunction.Sum(Sheet5.Range("D2:D1000")), "$#,##0")
.Font.Size = 9
.Font.Bold = True
.Left = 368
.Width = 50
.Height = 15
.Top = 342
End With
Code in the listbox double-click event that clears the worksheet values and removes the label. lbExists() is a function that checks to see whether or not the labels have been created yet.
Sheet5.Range("A2:I10000").Clear
Dim i3 As Integer
For i3 = 1 To 6
If lbExists(i3) Then Me.Controls.Remove ("CstTotal" & i3) Else
Next i3
Please let me know of any suggestions or if more information is needed. Thank you very much.
Button Click Code To create label and assign sum of values on worksheet to label's caption:
Dim CstTotal1 As Object
Set CstTotal1 = UserForm2.ProjectInfo.Add("Forms.Label.1", "CstTotal1", True)
With CstTotal1
.Caption = Format(Application.WorksheetFunction.Sum(Sheet5.Range("D2:D1000")), "$#,##0")
.Font.Size = 9
.Font.Bold = True
.Left = 368
.Width = 50
.Height = 15
.Top = 342
End With
Code in the listbox double-click event that clears the worksheet values and removes the label. lbExists() is a function that checks to see whether or not the labels have been created yet.
Sheet5.Range("A2:I10000").Clear
Dim i3 As Integer
For i3 = 1 To 6
If lbExists(i3) Then Me.Controls.Remove ("CstTotal" & i3) Else
Next i3
Please let me know of any suggestions or if more information is needed. Thank you very much.