2016-12-07 6 views
-1

Ich bin sehr auf dieses Coding-Projekt stecken und könnte wirklich etwas Hilfe verwenden.Warum erhalte ich einen #VALUE Fehler in Excel mit meinem Code? Werfen Sie einen Blick auf meinen Code

Ich versuche Code zu schreiben, der die Steuern auf das Einkommen einer Person berechnen wird. In diesem Modell hat mein Professor uns eine Liste der zu verwendenden Steuersätze und eine Reihe von Regeln gegeben, die ändern, wie Steuern berechnet werden. Ich habe stundenlang daran gearbeitet und bekomme nie die richtige Antwort.

Die Funktion gibt einen #VALUE Fehler in Excel zurück, und ich verstehe nicht warum.

'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 
+0

Willkommen bei SO! Ich habe den Code formatiert und ein wenig Fluff aus Ihrem Post entfernt, aber leider ist Ihre Frage nicht in der richtigen Form. Fragen fragen "Was stimmt nicht mit meinem Code?" muss ** das gewünschte Verhalten **, ein ** spezifisches Problem ** oder einen Fehler und den kürzesten Code enthalten, der notwendig ist, um es in der Frage selbst zu reproduzieren ([mcve]). Fragen ohne eine klare Problemstellung sind für andere Leser nicht nützlich. –

+0

Ihre Schleifenanzahl 'Für i = 1 To UBound (sizeArray, 1)' nimmt ein 2D-Array an, aber Sie verwenden nur 1-dimensionale Indizierung: 'taxRates (i)' – Comintern

+0

Vielen Dank für das herzliche Willkommen. Ich änderte den Titel, um zu sagen, dass ich Hilfe mit dem Fehler #VALUE benötige, den ich hereinziehe, wenn ich meine Funktion ausführe. Gibt es noch etwas, das ich mit meiner Post ändern muss? –

Antwort

1
AltTaxRate = taxRates(i) - 0.05 

Es gibt keine i wo Sie diese Zeile haben.

Wenn Sie i nicht deklariert haben, dann wird es einen Standardwert von Null haben, und Ihr Fehler ist, weil taxRates(0) nicht existiert.

+0

^Sieht so aus, als ob er diese Zeile innerhalb seiner for-Anweisung verschieben möchte. – ClintB

Verwandte Themen