-1

我非常坚持这个编码项目,真的可以使用一些帮助。

我正在尝试编写代码来计算一个人的收入应缴税款。在这个模型中,我的教授给了我们要使用的税率列表和一组改变税收计算方式的规则。我已经为此工作了几个小时,但永远无法得到它给我正确的答案。

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

'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
4

1 回答 1

1
AltTaxRate = taxRates(i) - 0.05

没有i你有这条线的地方。

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

于 2016-12-07T07:13:35.397 回答