Scope of private function in VBA Module

O

oscar.c.marin

A function declared Private in VBA Module doesn't run on UserForm. Error
reads: Sub or Function not defined.

Function:
Private Function IncomeTax(salaryGross, children, spouse) As Double
Application.ScreenUpdating = False
Call objectVarDeclare

Dim dblChildAmount As Double
Dim dblSpouseAmount As Double
Dim dblTaxAmount As Double
Dim dblTaxNet As Double
Dim rngTaxOne As Single
Set rngTaxOne = salarios_data.Range("tax1")
Dim rngTaxTwo As Single
Set rngTaxTwo = salarios_data.Range("tax2")
Dim rngMinSalary As Range
Set rngMinSalary = salariosData.Range("upper_first")
Dim rngFirstTier As Range
Set rngFirstTier = salarios_data.Range("upper_second")
Dim rngChildCredit As Range
Set rngChildCredit =
salarios_data.Range("child_credit")
Dim rngSpouseCredit As Range
Set rngSpouseCredit = salarios_data.Range("spouse_credit")

dblChildAmount = 0
dblSpouseAmount = 0
dblTaxAmount = 0
If salaryGross <= minSalary Then
dblTaxAmount = 0
IncomeTax = 0
dblChildAmount = 0
dblSpouse_amount = 0
ElseIf salaryGross > minSalary And salaryGross <= rngFirstTier Then
dblTaxAmount = ((salaryGross - minSalary) * rngTaxOne.Value)
Select Case children
Case Is > 0
dblChildAmount = children * rngChildCredit
Case Is = 0
dblChildAmount = 0
End Select
Select Case spouse
Case Is > 0
dblSpouseAmount = spouse * rngSpouseCredit
Case Is = 0
dblSpouseAmount = 0
End Select
If dblChildAmount + dblSpouseAmount >= dblTaxAmount
Then
IncomeTax = 0
Else: IncomeTax = dblTaxAmount - dblChildAmount - _
dblSpouseAmount
End If
ElseIf salaryGross > rngFirstTier Then
dblTaxAmount = ((rngFirstTier - rngMinSalary) * rngTaxOne.Value) + _
((salario - rngFirstTier) * rngTaxTwo.Value)
Select Case children
Case Is > 0
dblChildAmount = children * rngChildCredit
Case Is = 0
dblChildAmount = 0
End Select
Select Case spouse
Case Is > 0
dblSpouseAmount = spouse * rngSpouseCredit
Case Is = 0
dblSpouseAmount = 0
End Select
If dblChildAmount + dblSpouseAmount >= dblTaxAmount Then
IncomeTax = 0
Else: IncomeTax = dblTaxAmount - dblChildAmount -
dblSpouseAmount
End If
End If
End Function

In UserForm's OnChange event of ListBox of names of employees:
sngTaxNet = IncomeTax(rngGrossSalary.Offset(intIndex + 1, 0), _
rngChildren.Offset(intIndex + 1, 0), rngSpouse.Offset(intIndex + 1,
0))


Any way to make it work? Any mistakes made writing the code?

Thanks in advance.

Oscar
 
C

Chip Pearson

A function declared as Private is visible only to other procedures
within the same module. It won't be found by code in any other module.
You should declare the procedure as Public or omit the
Private/Public/Friend qualifier altogether. See
http://www.cpearson.com/Excel/Scope.aspx for more information about
the scope of variables and procedures.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top