Results 1 to 6 of 6

Thread: Finding Average of 3 Numbers excluding zeros

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    6

    Post Finding Average of 3 Numbers excluding zeros

    I am creating a application for my personal use. I have three textboxes in the form for inputting 3 numbers. I need to find the average of the three numbers entered in those textboxes.

    Below are the scenario's in my application;

    One textbox can have value and the other two will be left blank and value in single text box must be displayed as average

    Two textbox can contain value and the other can be blank. Average for the Values in two textboxes must be displayed.

    All the three text box can have value. Average for all the three values must be found.

    I coded for this but it is not working properly. Below is my code for the reference.

    Code:
     
    Public Sub Calculate_Avg_Rate()
        If Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
            If Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
                txtAvgRice.Text = Val(txtRiceC.Text)
            End If
        ElseIf Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
            If Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
                txtAvgRice.Text = Val(txtRiceB.Text)
            End If
        ElseIf Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
            If Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
                txtAvgRice.Text = Val(txtValue1.Text)
            End If
        ElseIf Val(txtValue1.Text) = "0" Or Len(txtValue1.Text) = 0 Then
            txtAvgRice.Text = Round(((Val(txtRiceB.Text) + Val(txtRiceC.Text)) / 2), 2)
        ElseIf Val(txtRiceB.Text) = "0" Or Len(txtRiceB.Text) = 0 Then
            txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceC.Text)) / 2), 2)
        ElseIf Val(txtRiceC.Text) = "0" Or Len(txtRiceC.Text) = 0 Then
            txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceC.Text)) / 2), 2)
        Else
            txtAvgRice.Text = Round(((Val(txtValue1.Text) + Val(txtRiceB.Text) + Val(txtRiceC.Text)) / 3), 2)
        End If
    End Sub
    I am calling the above procedure in the fourth textbox got focus event. The fourth textbox will display the average value when it is getting clicked.

    When I enter the value in the textboxes, the average is not getting populated. I have also attached the project for reference.

    Any help regarding is highly appreciated. Thanks in advance.



    --------------------------------------------------------------------------------

    Thanks you and Kind Regards, Karthik Venkatraman
    Attached Files Attached Files

Posting Permissions

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



Click Here to Expand Forum to Full Width