I am very stuck on this coding project and could really use some help.
I am trying to write code that will calculate the taxes due on a person's income. In this model, my professor has given us a list of tax rates to use and a set of rules that change how taxes are calculated. I have worked on this for hours and can never get it to give me the correct answer.
The function returns a #VALUE
error in Excel, and I don't understand why.
'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