Using If, Then, Else in a UDF

Rumpkin

Board Regular
Joined
Sep 24, 2016
Messages
75
Office Version
  1. 2019
Platform
  1. Windows
Hello and Happy New Year

I would like to change this formula to a UDF or Sub if possible:
IF($D16=0,"0",IF(INDEX(STDRATE,MATCH($C16,Resource_Code_LIST,0))>$AV16,
INDEX(STDRATE,MATCH($C16,Resource_Code_LIST,0)),IF(OR($C16=0,INDEX(
TOTAL_WAGES,MATCH($C16,Resource_Code_LIST,0),MATCH(
WAGE,WAGE_NAME,0))=0),"0",INDEX(TOTAL_WAGES,MATCH(
$C16,Resource_Code_LIST,0),MATCH(WAGE,WAGE_NAME,0)))))

Monster, right?
I need the Zero result to be in quotes as it copies to another program.
I tried this code but get a #Value
Code:
  [TABLE]
<colgroup><col style="width:60pt" width="80">  </colgroup><tbody>[TR]
   [TD="width: 80"]Function   BasePay(rng1 As Range, val1 As Variant, rng2 As Range, val2 As Variant, rng3   As Range, rng4 As Range, rng5 As Range)[/TD]
  [/TR]
  [TR]
   [TD]BasePay = 0[/TD]
  [/TR]
  [TR]
   [TD]On Error GoTo End1[/TD]
  [/TR]
  [TR]
   [TD]ElseIf WorksheetFunction.Index(rng1,   WorksheetFunction.Match(val1, rng2, 0)) > val2 Then[/TD]
  [/TR]
  [TR]
   [TD]      BasePay = WorksheetFunction.Index(rng1, WorksheetFunction.Match(val1,   rng2, 0))[/TD]
  [/TR]
  [TR]
   [TD]If WorksheetFunction.Index(rng1,   WorksheetFunction.Match(val1, rng2, 0)) = 0 Then[/TD]
  [/TR]
  [TR]
   [TD]      BasePay = WorksheetFunction.Index(rng1, WorksheetFunction.Match(val1,   rng2, 0), WorksheetFunction.Match(WorksheetFunction.Index(rng3,   WorksheetFunction.Match(val2, rng4, 0)), rng5, 0))[/TD]
  [/TR]
  [TR]
   [TD]End1:[/TD]
  [/TR]
  [TR]
   [TD]End Function[/TD]
  [/TR]
 </tbody>[/TABLE]
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
your IF statement is out of order....use IF then the ELSE....
(this assumes your WorksheetFunction is correct)

Code:
Function  BasePay(rng1 As Range, val1 As Variant, rng2 As Range, val2 As Variant, rng3 As Range, rng4 As Range, rng5 As Range)


BasePay = 0
On Error GoTo End1


If WorksheetFunction.Index(rng1, WorksheetFunction.Match(val1, rng2, 0)) > val2 Then
      BasePay = WorksheetFunction.Index(rng1, WorksheetFunction.Match(val1,   rng2, 0))
elseIf WorksheetFunction.Index(rng1,   WorksheetFunction.Match(val1, rng2, 0)) = 0 Then
      BasePay = WorksheetFunction.Index(rng1, WorksheetFunction.Match(val1, rng2, 0), WorksheetFunction.Match(WorksheetFunction.Index(rng3, WorksheetFunction.Match(val2, rng4, 0)), rng5, 0))
endif


End1:
End Function
 
Last edited:
Upvote 0
Thanks. I still have something missing. I get a #value .
This is my entry:
=BasePay(STDRATE,$C16,Resource_Code_LIST,$AV16,STDRATE,$C16,Resource_Code_LIST,$C16,TOTAL_WAGES,$C16,Resource_Code_LIST,WAGE,WAGE_NAME,TOTAL_WAGES,$C16,Resource_Code_LIST,WAGE,WAGE_NAME)
I'll be off line for a while. I recheck it when i get back.

Regards
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,834
Members
449,192
Latest member
mcgeeaudrey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top