Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

vb.net - VBA UDF returning #VALUE!

So I have a pretty simple UDF written in visual basic for use in excel. It calculates your approx. taxes. Lets say I use it as such:

=Taxes(I23-I18,I24-I20,"Married")

If I type this in it works great. Now if I save the sheet and restart excel the cell now says #VALUE! If I select the formula and hit enter once again it recalculates it fine. What am I doing wrong? Application.Volatile shouldn't be needed but I was trying ideas...

Private Type TaxBracket
    Perc As Double
    Floor As Currency
    Limit As Currency
End Type

Public Function Taxes(gross1 As Currency, gross2 As Currency, filingStatus As String) As Currency
    Application.Volatile True
    Dim brackets(6) As TaxBracket
    Dim stdDeduction As Currency
    Dim ssTaxRate As Double
    Dim medicareTaxRate As Double
    Dim Tax As Double

    stdDeduction = 5700
    ssTaxRoof = 106800
    ssTaxRate = 0.062
    medicareTaxRate = 0.0145

    Tax = medicareTaxRate * (gross1 + gross2)
    Tax = Tax + IIf(gross1 < ssTaxRoof, ssTaxRate * gross1, ssTaxRate * ssTaxRoof)
    Tax = Tax + IIf(gross2 < ssTaxRoof, ssTaxRate * gross2, ssTaxRate * ssTaxRoof)

    brackets(0).Perc = 0.1
    brackets(1).Perc = 0.15
    brackets(2).Perc = 0.25
    brackets(3).Perc = 0.28
    brackets(4).Perc = 0.33
    brackets(5).Perc = 0.35

    If filingStatus = "Single" Then
        brackets(0).Floor = 0
        brackets(1).Floor = 8375
        brackets(2).Floor = 34000
        brackets(3).Floor = 82400
        brackets(4).Floor = 171850
        brackets(5).Floor = 373650
        brackets(0).Limit = 8375
        brackets(1).Limit = 34000
        brackets(2).Limit = 82400
        brackets(3).Limit = 171850
        brackets(4).Limit = 373650
        brackets(5).Limit = 1000000000

        Tax = Tax + incomeTaxes(gross1, brackets, stdDeduction) + incomeTaxes(gross2, brackets, stdDeduction)
    ElseIf filingStatus = "Married" Then
        brackets(0).Floor = 0
        brackets(1).Floor = 16750
        brackets(2).Floor = 68000
        brackets(3).Floor = 137300
        brackets(4).Floor = 209250
        brackets(5).Floor = 373650
        brackets(0).Limit = 16750
        brackets(1).Limit = 68000
        brackets(2).Limit = 137300
        brackets(3).Limit = 209250
        brackets(4).Limit = 373650
        brackets(5).Limit = 1000000000

        Tax = Tax + incomeTaxes(gross1 + gross2, brackets, stdDeduction * 2)
    Else
        Taxes = "N/A"
        Return
    End If
    Taxes = Tax
End Function

Private Function incomeTaxes(gross As Currency, brackets() As TaxBracket, deduction As Currency) As Currency
    Dim Tax As Double
    Dim taxable As Double

    Tax = 0
    taxable = gross - deduction


    For i = 0 To 5
      If taxable > brackets(i).Limit Then
        Tax = Tax + (WorksheetFunction.Min(taxable, brackets(i).Limit) - brackets(i).Floor) * brackets(i).Perc
      Else
        If taxable > brackets(i).Floor Then
            Tax = Tax + (taxable - brackets(i).Floor) * brackets(i).Perc
        Else
            'tax = tax
        End If
      End If
    Next i

    incomeTaxes = Tax
End Function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your UDF's look OK, apart from using Currency data types (probably should be using doubles or variants since that is what Excel uses).
The usual reason for getting #Value with a UDF is that one of the input arguments cannot be converted to the correct type. If your input cells do not contain numeric values when the workbook opens you would get #Value.
This might be caused by calculation sequence problems resulting in one of the upstream precedent cells being uncalculated the first time the function is called.
Try declaring the input parameters as variant rather than currency and add some temporary debug.print statements to show the input parameters in the Immediate window.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...