2015-12-31 98 views
0

努力創造我的第一個UDF,我不斷收到編譯錯誤:不定義Sub或功能。完全自學成才,將其作爲愛好嘗試。我得到突出顯示這個錯誤的第一行,這裏是我的代碼。編譯錯誤,子或函數定義

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 

我知道這只是一堆嵌套的if()的,但我想先從簡單的東西,我會VLOOKUP內這樣做()的。這是我基於此的圖表。 What my function is quantifying基本上,當與定義屬性LVL,期望的技能拉特,和技術人員的難度提出只是搞清楚技能水平成本。

+1

查找到'選擇Case'它將使這個很多更容易閱讀。 –

回答

2

拼寫錯誤的位置:

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

@DanielPadia - 添加'選項Explicit'使得這樣一個非問題錯別字... – Comintern

1

按我的評論看到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 

這只是在這裏以供參考。 @ Gary'sStudent指出錯誤。