+ Reply to Thread
Results 1 to 4 of 4

Averaging Text box values excluding blanks in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    6

    Averaging Text box values excluding blanks in VBA

    Hello,

    This is my second post on this forum, and I apologize if I do anything incorrectly (I have read the rules and am making every attempt to follow them).

    I have created a sample file (TEST1 attached) to illustrate my questions. In the userform, I have textbox5 averaging when data is entered into textboxes 1-4. It seems like an easy fix, but I cannot figure it out today.

    1.) How do I change the calculation to average only the cells with values in them?
    2.) Does the code have to be input into each textbox_change command in order to work or is there a more efficient way?

    That seems like a good place to start. Thank you in advance for your time and assistance.

    Nolan
    Attached Files Attached Files
    Last edited by nhannigan; 12-28-2010 at 03:28 PM.

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Averaging Text box values excluding blanks in VBA

    1) see the code below. It checks that a textbox contains a number before it adds it to the runnign sum.
    2) there are several ways to do this, but all generate about the same amount of code. VBA will not allow you to share event handlers between controls.
    Private Sub CommandButton1_Click()
    UserForm1.Hide
    End Sub
    
    Private Sub TextBox1_Change()
    DoCalc
    End Sub
    Private Sub TextBox2_Change()
    DoCalc
    End Sub
    Private Sub TextBox3_Change()
    DoCalc
    End Sub
    Private Sub TextBox4_Change()
    DoCalc
    End Sub
    
    ' check if a text string converts to a double
    Private Function CheckValue(myText As String) As Boolean
    
    On Error Resume Next
        Dim myValue As Double
        myValue = CDbl(myText)
        If (Err.Number <> 0) Then
            CheckValue = False
        Else
            CheckValue = True
        End If
    On Error GoTo 0
    
    End Function
    Sub DoCalc()
    Dim mySum As Double
    Dim N As Long
    mySum = 0
    N = 0
    If (CheckValue(Me.TextBox1.Text)) Then
        mySum = mySum + Val(Me.TextBox1.Value)
        N = N + 1
    End If
    If (CheckValue(Me.TextBox2.Text)) Then
        mySum = mySum + Val(Me.TextBox2.Value)
        N = N + 1
    End If
    If (CheckValue(Me.TextBox3.Text)) Then
        mySum = mySum + Val(Me.TextBox3.Value)
        N = N + 1
    End If
    If (CheckValue(Me.TextBox4.Text)) Then
        mySum = mySum + Val(Me.TextBox4.Value)
        N = N + 1
    End If
    If (N > 0) Then
        TextBox5.Value = mySum / N
    Else
        TextBox5.Text = ""
    End If
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    Last edited by davesexcel; 12-28-2010 at 03:12 PM. Reason: Quoting previous post is not necessary
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Averaging Text box values excluding blanks in VBA

    Try this...

    Sub DoCalc()
    Dim FullCount As Long
    Dim MySum As Single
    If Me.TextBox1.Value <> "" Then
        FullCount = FullCount + 1
        MySum = MySum + Me.TextBox1.Value
    End If
    If Me.TextBox2.Value <> "" Then
        FullCount = FullCount + 1
        MySum = MySum + Me.TextBox2.Value
    End If
    If Me.TextBox3.Value <> "" Then
        FullCount = FullCount + 1
        MySum = MySum + Me.TextBox3.Value
    End If
    If Me.TextBox4.Value <> "" Then
        FullCount = FullCount + 1
        MySum = MySum + Me.TextBox4.Value
    End If
    
    If FullCount > 0 Then
        TextBox5.Value = MySum / FullCount
    Else
        TextBox5.Value = ""
    End If
    End Sub

  4. #4
    Registered User
    Join Date
    10-08-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Averaging Text box values excluding blanks in VBA

    SOLVED. Both work perfectly. Blane245, it is good to know that the method I have been using is pretty much as good as it gets.

    Thank you both for your quick responses.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1