Daniel Padia
Daniel Padia

Reputation: 31

Compile Error, Sub or Function not defined

trying to create my first UDF, I keep getting Compile error: Sub or Function not defined. completely self taught, trying this out as a hobby. I get the first line highlighted on this error, here's my code.

Function SkillCost(AttrLVL, SkillLVL, E_A_H)
 If E_A_H = "E" Then
  If SkillLVL - AttrLVL < 0 Then
   SkillCost = 0
  Else
   If SkillLVL - AttrLVL < 1 Then
    SkillCost = 1
   Else
    If SkillLVL - AttrLVL < 2 Then
     SkillCost = 2
    Else
     Skill Cost = ((SkillLVL - AttrLVL) - 1) * 4
    End If
   End If
  End If
 Else
  If E_A_H = "A" Then
   If SkillLVL - AttrLVL < -1 Then
    SkillCost = 0
   Else
    If SkillLVL - AttrLVL < 0 Then
     SkillCost = 1
    Else
     If SkillLVL - AttrLVL < 1 Then
      SkillCost = 2
     Else
      SkillCost = (SkillLVL - AttrLVL) * 4
     End If
    End If
   End If
  Else
   If E_A_H = "H" Then
    If SkillLVL - AttrLVL < -2 Then
     SkillCost = 0
    Else
     If SkillLVL - AttrLVL < -1 Then
      SkillCost = 1
     Else
      If SkillLVL - AttrLVL < 0 Then
       SkillCost = 2
      Else
       SkillCost = ((SkillLVL - AttrLVL) + 1) * 4
      End If
     End If
    End If
   End If
  End If
 End If
End Function

I know this is just a bunch of nested if()'s, but I wanted to start with something simple, and I would be doing this within vlookup()'s. Here's the chart I'm basing this off of. What my function is quantifying basically, just figuring out skill level cost when presented with defining attribute lvl, desired skill lvl, and difficulty of skill.

Upvotes: 0

Views: 148

Answers (2)

Scott Craner
Scott Craner

Reputation: 152505

As per my comment look into Select Case:

Function SkillCost(AttrLVL, SkillLVL, E_A_H)
 If E_A_H = "E" Then
    Select Case SkillLVL - AttrLVL
        Case Is < 0
            SkillCost = 0
        Case Is < 1
            SkillCost = 1
        Case Is < 2
            SkillCost = 2
        Case Else
            SkillCost = ((SkillLVL - AttrLVL) - 1) * 4
    End Select
 ElseIf E_A_H = "A" Then
    Select Case SkillLVL - AttrLVL
        Case Is < -1
            SkillCost = 0
        Case Is < 0
            SkillCost = 1
        Case Is < 1
            SkillCost = 2
        Case Else
            SkillCost = ((SkillLVL - AttrLVL) - 1) * 4
    End Select
ElseIf E_A_H = "H" Then
    Select Case SkillLVL - AttrLVL
        Case Is < -2
            SkillCost = 0
        Case Is < -1
            SkillCost = 1
        Case Is < 0
            SkillCost = 2
        Case Else
            SkillCost = ((SkillLVL - AttrLVL) - 1) * 4
    End Select
End If
End Function

This is just here for reference. @Gary'sStudent pointed out the error.

Upvotes: 1

Gary&#39;s Student
Gary&#39;s Student

Reputation: 96753

Spelling error here:

 Skill Cost = ((SkillLVL - AttrLVL) - 1) * 4

Upvotes: 2

Related Questions