我在嘗試計算6個項目的總體平均值。其中一些項目可能爲零,因此在計算平均值時不會發揮作用。平均值完全讓我感到困惑,並且在此過程中遇到困難。基於數組計算AVG
我的代碼,因爲我現在有它:
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
這是我試圖獲得平均我的代碼的相關部分。
我有作爲一個例子:
- 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
基於該信息的上方,只第2項(tbAdjCostPerGal
)將被添加,然後平均,因爲項目3-6是一個0值,結束w最終每加侖平均成本爲2.364美元。
所以總而言之,我需要總共和平均所有的tbAdjCostPerGal.text > 0
。我相信如果我平均只有tbAdjCostPerGal
條目,這將給我我正在尋找的答案。每條記錄總是有6個條目。
我知道我需要創建一些方法來創建一個變量來查看tbAdjCostPerGal
條目,總計只有那些值大於0的條目,這是我缺乏知識的地方。有人可以幫助我正確地格式化這個嗎?
零是一個合法的值 - 爲什麼它不會發揮作用? – Plutonix
看起來這是平均燃料價格。我猜燃料價格爲0.0美元意味着沒有價格記錄而不是燃料是免費的。 (我必須去哪裏填補這些燃料?)因此,在計算平均燃料成本的情況下,您確實想要排除$ 0值。在這種情況下,應該使用更好的「沒有價格記錄」的信號量。但是因爲我們不需要使用0.0美元的信號量。 – David