infinitedrifter
New Member
I'm having trouble with a custom function that pulls an amount from a worksheet based on factors entered on the main sheet (age, zip, etc). I've updated these on several different workbooks(we have one for each state) before with no problem but this week none of the ones I've added seem to work. I'm afraid I'm just making the same mistake in the coding on each one & being that I'm a beginner w/ VBA (more accustomed to using formulas) I just haven't found the error.
Here's an example of one of the function codes:
[pre]<br />
Function Constitution_RATE_PLAN(ByRef zip As Range, ByRef Gender As Range, ByRef Tobacco As Range, ByRef Age As Long, ByRef pay_method As Range, ByRef rate_plan As Range) As Double </p>
<p> 'Initially we need to determine the rate plan that was selected.<br />
Dim rate_str As String </p>
<p> rate_str = "Constitution_" & replace_spaces(Trim(rate_plan), "_"
</p>
<p> 'Payment method factor.<br />
Dim pay_factor As Double </p>
<p> Select Case pay_method<br />
Case "Annually"<br />
pay_factor = 1<br />
Case "SemiAnnually"<br />
pay_factor = 0.52<br />
Case "Quarterly"<br />
pay_factor = 0.265<br />
Case "Monthly"<br />
pay_factor = 0.085<br />
End Select </p>
<p> 'Determine gender pay factor.<br />
rate_str = rate_str & "_" & Gender </p>
<p> 'Determine tobacco factor.<br />
rate_str = rate_str & "_" & Tobacco </p>
<p> 'Determine the area factor.<br />
Dim Area_range1 As Range, Area_range2 As Range </p>
<p> Set Area_range1 = Constitution.Range("Constitution_Area_1"
<br />
'Set Area_range2 = Constitution.Range("Constitution_Area_2"
</p>
<p> 'Now we search the range for the appropriate column.<br />
If factor_search(Area_range1, CLng(Left(Trim(zip), 3))) Then<br />
rate_str = rate_str & "_" & 1<br />
Else: rate_str = rate_str & "_" & 2<br />
End If </p>
<p> 'Now we find the plan.<br />
Dim current_plan As Range<br />
Set current_plan = Constitution.Range(rate_str)<br />
'Now we need to find the rate according to the age and the factors found above.<br />
Dim age_row As Long </p>
<p> If Age <= 64 Then<br />
age_row = 1<br />
Else: age_row = Age_search(Constitution.Range("Constitution_Age"
, Age)<br />
End If </p>
<p> Constitution_RATE_PLAN = current_plan.Cells(age_row, 1) * pay_factor </p>
<p>End Function<br />
On the main sheet the result is found using this code:
<br />
=If(ISERROR(Constitution_RATE_PLAN(Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1)),"N/A",uOO_RATE_PLAN(Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1))<br />
[/pre]
Uploaded here: https://docs.google.com/leaf?id=0B9_U_YkSNyAtYjUxMzc4ZTAtNWZhOS00MTA4LTg2YzYtYTRlZGQxMTg1YTYz&hl=en_US
- The (Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1) parts are all in working order because they return correct results for the already existing functions (we have 1 for each company/spreadsheet, i.e. instead of "Constitution_Rate_Plan" it's "AARP_Rate_Plan")
- The Constitution spreadsheet is organized with named ranges which allow the Function to search for the variables entered on the first sheet & return the correct rate. I suspect this is where the error is happening, somewhere in the Define Area or Age portion but I haven't been able to find a solution. I hope this will reveal to someone smarter than I what I'm doing wrong.
- I know IFERROR is more efficient but the original code was created by someone else, probably in an earlier version of Excel & I haven't gotten around to cleaning it up.
Thanks for any insight you might be able to provide. I think if I stare at this any more my brain will explode.
Here's an example of one of the function codes:
[pre]<br />
Function Constitution_RATE_PLAN(ByRef zip As Range, ByRef Gender As Range, ByRef Tobacco As Range, ByRef Age As Long, ByRef pay_method As Range, ByRef rate_plan As Range) As Double </p>
<p> 'Initially we need to determine the rate plan that was selected.<br />
Dim rate_str As String </p>
<p> rate_str = "Constitution_" & replace_spaces(Trim(rate_plan), "_"
<p> 'Payment method factor.<br />
Dim pay_factor As Double </p>
<p> Select Case pay_method<br />
Case "Annually"<br />
pay_factor = 1<br />
Case "SemiAnnually"<br />
pay_factor = 0.52<br />
Case "Quarterly"<br />
pay_factor = 0.265<br />
Case "Monthly"<br />
pay_factor = 0.085<br />
End Select </p>
<p> 'Determine gender pay factor.<br />
rate_str = rate_str & "_" & Gender </p>
<p> 'Determine tobacco factor.<br />
rate_str = rate_str & "_" & Tobacco </p>
<p> 'Determine the area factor.<br />
Dim Area_range1 As Range, Area_range2 As Range </p>
<p> Set Area_range1 = Constitution.Range("Constitution_Area_1"
'Set Area_range2 = Constitution.Range("Constitution_Area_2"
<p> 'Now we search the range for the appropriate column.<br />
If factor_search(Area_range1, CLng(Left(Trim(zip), 3))) Then<br />
rate_str = rate_str & "_" & 1<br />
Else: rate_str = rate_str & "_" & 2<br />
End If </p>
<p> 'Now we find the plan.<br />
Dim current_plan As Range<br />
Set current_plan = Constitution.Range(rate_str)<br />
'Now we need to find the rate according to the age and the factors found above.<br />
Dim age_row As Long </p>
<p> If Age <= 64 Then<br />
age_row = 1<br />
Else: age_row = Age_search(Constitution.Range("Constitution_Age"
End If </p>
<p> Constitution_RATE_PLAN = current_plan.Cells(age_row, 1) * pay_factor </p>
<p>End Function<br />
On the main sheet the result is found using this code:
<br />
=If(ISERROR(Constitution_RATE_PLAN(Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1)),"N/A",uOO_RATE_PLAN(Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1))<br />
[/pre]
Uploaded here: https://docs.google.com/leaf?id=0B9_U_YkSNyAtYjUxMzc4ZTAtNWZhOS00MTA4LTg2YzYtYTRlZGQxMTg1YTYz&hl=en_US
- The (Household_Discounts, Zip_Code,Gender,Tobacco,Age,Payment_Method,Plan_Option1) parts are all in working order because they return correct results for the already existing functions (we have 1 for each company/spreadsheet, i.e. instead of "Constitution_Rate_Plan" it's "AARP_Rate_Plan")
- The Constitution spreadsheet is organized with named ranges which allow the Function to search for the variables entered on the first sheet & return the correct rate. I suspect this is where the error is happening, somewhere in the Define Area or Age portion but I haven't been able to find a solution. I hope this will reveal to someone smarter than I what I'm doing wrong.
- I know IFERROR is more efficient but the original code was created by someone else, probably in an earlier version of Excel & I haven't gotten around to cleaning it up.
Thanks for any insight you might be able to provide. I think if I stare at this any more my brain will explode.