Results 1 to 6 of 6

Thread: Finding Average of 3 Numbers excluding zeros

Threaded View

  1. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Finding Average of 3 Numbers excluding zeros

    You may want to try somethign more like this
    Code:
    If Val(txtValueA.Text) = 0 Then
        If Val(txtValueB.Text) = 0 Then
            If Val(txtValueC.Text) = 0 Then
                'No Valid entries above 0
                txtAverage.Text ="0"
            Else
                txtAverage.Text = Val(txtValueC.Text)
            End If
        Else
            If Val(txtValueC.Text) = 0 Then
                'Only b is valid
                txtAverage.Text = Val(txtValueB.Text)
            Else 'B and C are valid
                txtAverage.Text = Round(((Val(txtValueB.Text) + Val(txtValueC.Text)) / 2), 2)
            End If
        End If
    Else
        If Val(txtValueB.Text) = 0 Then
            If Val(txtValueC.Text) = 0 Then
                'Only A is valid
                txtAverage.Text = Val(txtValueA.Text)
            Else 'Only A and C are valid
                txtAverage.Text = Round(((Val(txtValueA.Text) + Val(txtValueC.Text)) / 2), 2)
            End If
        Else
            If Val(txtValueC.Text) = 0 Then
                'Only a and b are valid
                txtAverage.Text = Round(((Val(txtValueB.Text) + Val(txtValueB.Text)) / 2), 2)
            Else ' all are valid
                txtAverage.Text = Round(((Val(txtValueA.Text) + Val(txtValueB.Text) + Val(txtValueC.Text)) / 3), 2)
            End If
        End If
    End If
    Edit: Never mind, the post above is a better way to go.
    You still may want to look over the If structure in this post though to get an idea of the logic
    Last edited by DataMiser; Aug 17th, 2012 at 12:32 PM.

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