Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Scope of private function in VBA Module
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Scope of private function in VBA Module
Answered in other post.
-- __________________________________ HTH Bob "oscar.c.marin" wrote in message ... 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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Scope of private function in VBA Module
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) On Mon, 20 Oct 2008 13:27:11 -0700, oscar.c.marin wrote: 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
scope of private function called from UserForm fails | Excel Programming | |||
Improve method of calling a private function in a private module | Excel Programming | |||
private variable: same module, other Sub/Function | Excel Programming | |||
Call Private Function from ThisWorkbook module | Excel Programming | |||
Class Module Scope | Excel Programming |