Consulting

Results 1 to 11 of 11

Thread: Sum values vba code

  1. #1

    Sum values vba code

    Hello! I have a quote tool that I am developing with VBA. I need a place for users to add footages of pipe. I know that i can put a ton of textboxes on a sheet and then place a command button that would add add them all up but realistically, I would need a ton of those...

    What I'd like for a user to do is be able to keep a running tally of numbers in a textbox and then be able to add those when ready. Text entered might look something like this (pretend the brackets represent the textbox...) [27, 48, 90, 122, 45, 67]. Any suggestions on how to do that? Please keep in mind that I am a rookie to VBA. Getting better but still figuring it out

    Thank you in advance!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why noy use the Excel grid?

    Have the users add the, in column A say on a sheet, and have a cell that sums column A ad read that in your VBA.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    It just wouldn't work with how we have the userforms set up

  4. #4
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sub AddTxt()
        Dim i As Long, l As Long, arr As Variant
        arr = Split(Sheet1.TextBox1.Text, ",")
        For i = 0 To UBound(arr)
            l = l + Val(arr(i))
        Next
        MsgBox l
    End Sub
    Semper in excretia sumus; solum profundum variat.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Ryanchris View Post
    It just wouldn't work with how we have the userforms set up
    Then change it, use the best tools for the job. That is what design is all about.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Paul - where do I apply this code? Do I need to create a command button (one that says "Calculate" for example and place the code with that?

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    $User actions = Type in Textbox, press Enter, type in textbox, press Enter, Repeat as needed

    In UserForm CodePage
    Dim TextBox1Total As Double
    
    Private Sub TextBox1_Enter()
       TextBox1Total = 0
    End Sub
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
       Cancel = True
       TextBox1Total = TextBox1Total + CDbl(Me.TextBox1)
       Me.TextBox1 = ""
    MsgBox CStr(TextBox1Total)
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    @Ryanchris

    Yes create a command button and if it's the first then

    Private Sub CommandButton1_Click()
        Dim i As Long, l As Long, arr As Variant
        arr = Split(TextBox1.Text, ",")
        For i = 0 To UBound(arr)
            l = l + Val(arr(i))
        Next
        MsgBox l
    End Sub
    Change the red Textbox1 to whatever text box it is.

    Or, As Sam suggests, you can do it on TextBox_Enter/Exit and have 'live' updates.
    Semper in excretia sumus; solum profundum variat.

  9. #9
    Yes!! Thank you all! Works brilliantly. Really appreciate all of your help - best wishes to everyone.

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    You're welcome
    Semper in excretia sumus; solum profundum variat.

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •