Excel UDF Function if Statement giving me error

coach z 96 Reputation points
2021-05-21T22:32:36.547+00:00

Hello , The UDF function works only for one of the conditions and errors if there is more than one, using else if did not want to work, What am I doing wrong? Thank you

Function Calc(rowaddress)
If (cells(rowaddress.row,6).value="F" or cells(rowaddress.row,6).value="P" _
And cells(rowaddress.row,16).value=0 _
And cells(rowaddress.row,10).value<24) _
then Calc=24-cells(rowaddress.row,10).value
Else If (cells(rowaddress.row,6).value="F" or cells(rowaddress.row,6).value="O" _
And cells(rowaddress.row,16).value<>0 _
And cells(rowaddress.row,10).value>24) _
then Calc=24+cells(rowaddress.row,10).value
else calc=0

I inherited a long formula that a function will do wonders with it, it has 9 different conditions, any help will be greatly appreciated.

Thank you

Windows for business Windows Client for IT Pros User experience Other
{count} votes

Accepted answer
  1. coach z 96 Reputation points
    2021-05-22T19:52:57.79+00:00

    It is amazing how “else” in separate line makes the difference, your help was really appreciated and I would love to buy you lunch if I can, you made my day.
    Thank you
    Coach z


2 additional answers

Sort by: Most helpful
  1. coach z 96 Reputation points
    2021-05-22T03:11:06.91+00:00

    Thank you.
    Error says end without if block, when I look at the code the system change my Else if to Else: If.
    The formula works only when I leave only the first condition and remove the others, others comes with #value! In the cell result.
    Appreciate your help.

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2021-05-22T12:36:54.323+00:00

    VBA is ElseIf.

    https://www.educba.com/vba-else-if/

    Function Calc(rowaddress)
        If ((cells(rowaddress.row,6).value="F" or cells(rowaddress.row,6).value="P")
                And cells(rowaddress.row,16).value=0
                And cells(rowaddress.row,10).value<24)
        then 
            Calc=24-cells(rowaddress.row,10).value
        ElseIf ((cells(rowaddress.row,6).value="F" or cells(rowaddress.row,6).value="O")
                And cells(rowaddress.row,16).value<>0
                And cells(rowaddress.row,10).value>24)
        then 
            Calc=24+cells(rowaddress.row,10).value
        else 
            calc=0
        End if
    End Function  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.