I am attempting to calculate an overall average of 6 items. Some of these items may be zero therefore would not come into play when calculating the average. Averaging completely confuses me and am having a difficult time working through this.
My Code as I currently have it:
Dim a,
af1, af2, af3, af4, af5, af6,
t7, t8, t9, t10, t11, t12 As Decimal
'Adjusted Fuel Cost per line
af1 = CDec(IIf(tbAdjCostPerGal1.Text.Trim = "", 0D, tbAdjCostPerGal1.Text.Trim))
af2 = CDec(IIf(tbAdjCostPerGal2.Text.Trim = "", 0D, tbAdjCostPerGal2.Text.Trim))
af3 = CDec(IIf(tbAdjCostPerGal3.Text.Trim = "", 0D, tbAdjCostPerGal3.Text.Trim))
af4 = CDec(IIf(tbAdjCostPerGal4.Text.Trim = "", 0D, tbAdjCostPerGal4.Text.Trim))
af5 = CDec(IIf(tbAdjCostPerGal5.Text.Trim = "", 0D, tbAdjCostPerGal5.Text.Trim))
af6 = CDec(IIf(tbAdjCostPerGal6.Text.Trim = "", 0D, tbAdjCostPerGal6.Text.Trim))
'Truck Gallons Purchased Related
t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim))
t8 = CDec(IIf(tbTrkGalsPurch2.Text.Trim = "", 0D, tbTrkGalsPurch2.Text.Trim))
t9 = CDec(IIf(tbTrkGalsPurch3.Text.Trim = "", 0D, tbTrkGalsPurch3.Text.Trim))
t10 = CDec(IIf(tbTrkGalsPurch4.Text.Trim = "", 0D, tbTrkGalsPurch4.Text.Trim))
t11 = CDec(IIf(tbTrkGalsPurch5.Text.Trim = "", 0D, tbTrkGalsPurch5.Text.Trim))
t12 = CDec(IIf(tbTrkGalsPurch6.Text.Trim = "", 0D, tbTrkGalsPurch6.Text.Trim))
'Calculate ADJUSTED Average Cost of ALL Fuel plus Fuel Card fee and any Fuel Discounts this Load
Try
If af1 > 0 Then
a = (af1 + af2 + af3 + af4 + af5 + af6) / (t7 + t8 + t9 + t10 + t11 + t12)
tbFuelCostAdj.Text = a.ToString("C3")
Else
a = 0D
tbFuelCostAdj.Text = a.ToString("C3")
End If
Catch ex As Exception
'If a calculation error occurs, show Error message box
Dim frm As New MeMsgCalcError(ex, "'Adjusted Cost of Fuel this Load' Calculation Error." & vbCrLf & "Lines 644-652")
frm.Show()
End Try
This is the relevant portion of my code where I am attempting to get the average.
I have as an example:
- tbAdjCostPerGal1 = $2.259
- tbAdjCostPerGal2 = $2.469
- tbAdjCostPerGal3 = $0.000
- tbAdjCostPerGal4 = $0.000
- tbAdjCostPerGal5 = $0.000
tbAdjCostPerGal6 = $0.000
tbTrkGalsPurch1 = 100.000
- tbTrkGalsPurch2 = 93.000
- tbTrkGalsPurch3 = 0.000
- tbTrkGalsPurch4 = 0.000
- tbTrkGalsPurch5 = 0.000
- tbTrkGalsPurch6 = 0.000
Based on the info above, only the 1st 2 entries (tbAdjCostPerGal) would be added and then averaged since items 3-6 are a 0 value ending with an end result of $2.364 cost per gallon average.
So in summary, I need to total and average where all tbAdjCostPerGal.text > 0. I believe if I average just the tbAdjCostPerGal entries this would give me the answer I am looking for. There will always be 6 entries per record.
I know I need to create some way to make a variable to look at the tbAdjCostPerGal entries, totalling only those with a value greater than 0 and this is where I lack knowledge. Can someone assist me in the proper formatting of this please?