alvo alvo - 1 year ago 45 Question

Finding the sum of characters in a text box

I have a text box with characters got from a calculation in my code . the textbox specifically contains only integers... Is there a way I can sum up the integers in the text box? Eg. If my textbox has 123456, the code should find the sum of 1+2+3+4+5+6 and then display the sum in another text box. Thank you in advance

Answer Source


Public Sub Calculate()
    Dim i       As Integer
    Dim sum     As Integer
    Dim length  As Integer
    i = 1
    sum = 0
    length = Len(TextBox1.Text)

    While i <= length
        sum = sum + Mid(TextBox1.Text, i, 1)
        i = i + 1

    TextBox2.Text = sum
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download