为什么我的代码在excel中出现#VALUE错误?看看我的代码

时间:2016-12-07 03:37:56

标签: excel vba excel-vba finance tax

我非常依赖这个编码项目,可以真正使用一些帮助。

我正在尝试编写代码来计算应付个人收入的税款。在这个模型中,我的教授给了我们一份使用的税率清单和一套改变税收计算方法的规则。我已经为此工作了好几个小时,但却无法让它给我正确的答案。

该函数在Excel中返回#VALUE错误,我不明白为什么。

'This function calculate the tax liability based on taxable income (AGI)
Function CalculateTax(taxableIncome As Variant, taxRates As Variant, _
           taxThresholds As Variant, standardDeduction As Variant, _
           personalException As Variant, AlternativeTaxSystem As Variant, _
                                             bequest As Variant) As Variant
'Initiate a variable to use to determine the size of taxRates - for some reason doing this directly doesn't work
Dim sizeArray As Variant
sizeArray = taxRates
'Initiate a variable for the alternative tax system
Dim AltTaxRate As Double
AltTaxRate = taxRates(i) - 0.05
'Initiate variable and calculate amount to apply to tax table, leaving out the deductions from the equation
Dim amountToTax As Double
amountToTax = taxableIncome
'Initiate tax amount and set to zero
Dim taxAmount As Double
taxAmount = 0

'loop over tax brackets, adding the incremental tax each time
For i = 1 To UBound(sizeArray, 1)
    'Runs an if statement to check if all three conditions are met
    If AlternativeTaxSystem = "Yes" And bequest = "No" And taxRates(i) >= 0.2 Then
        'Calculate the tax amount per bracket, based on the minimum of the bracket size or total tax minus the bracket threshold, with a true minimum at zero, and subtracting off 0.05 from the tax rates
        taxAmount = taxAmount + Application.Max(Application.Min(taxThresholds(i + 1) - taxThresholds(i), amountToTax - taxThresholds(i)), 0) * AltTaxRate
    ElseIf AlternativeTaxSystem = "Yes" And bequest = "No" And taxRates(i) < 0.2 Then
        taxAmount = taxAmount + Application.Max(Application.Min(taxThresholds(i + 1) - taxThresholds(i), amountToTax - taxThresholds(i)), 0) * taxRates(i)
    ElseIf AlternativeTaxSystem = "Yes" And bequest = "Yes" And taxRates(i) >= 0.2 Then
        amountToTax = taxableIncome - 250000
        taxAmount = taxAmount + Application.Max(Application.Min(taxThresholds(i + 1) - taxThresholds(i), amountToTax - taxThresholds(i)), 0) * AltTaxRate
    ElseIf AlternativeTaxSystem = "Yes" And bequest = "Yes" And taxRates(i) < 0.2 Then
        amountToTax = taxableIncome - 250000
        taxAmount = taxAmount + Application.Max(Application.Min(taxThresholds(i + 1) - taxThresholds(i), amountToTax - taxThresholds(i)), 0) * taxRates(i)
    Else
        amountToTax = taxableIncome - standardDeduction - personalException
        taxAmount = taxAmount + Application.Max(Application.Min(taxThresholds(i + 1) - taxThresholds(i), amountToTax - taxThresholds(i)), 0) * taxRates(i)
    End If
Next i
'output answer to function
CalculateTax = taxAmount

End Function

1 个答案:

答案 0 :(得分:1)

AltTaxRate = taxRates(i) - 0.05

没有i你有这条线。

如果您尚未声明i,那么它的默认值为零,而您的错误是因为taxRates(0)不存在。