Create new Function in Excel

2020-05-03 11:34发布

I created a "IF Formula" in excel and wanted to convert that formula in function I read multiple article; but unable to convert formula into function: Formula is as follows:

 =IF(LEFT(L3,4)=0321," 12 - ABC type",
 IF(LEFT(L3,3)=021," 543 - XYZ type",
 IF(ISERROR(SEARCH("T56_",L3))=FALSE,MID(L3,SEARCH("T56_",L3),19),
 IF(ISERROR(SEARCH("MCW",L3))=FALSE,MID(L3,SEARCH("MCW_",L3),10),
 "Logic Missing"))))

I just include the 4 IF's from the formula; but I have way more than 10 IF logic in the actual formula. Is there a way to create this IF formula in Function.

I already tried the below mentioned code but it gave me #VALUE! error.

Function AccountType(dCell As Variant) As Long
If dCell = Left(dCell, 4) = "0321" Then
 AccountType = "12 - ABC type
ElseIf dCell = Left(dCell, 3) = "021" Then
 AccountType = "543 - XYZ type"
Else
 AccountType = "Logic Missing"
        End If
End Function

标签: excel vba
1条回答
三岁会撩人
2楼-- · 2020-05-03 11:59

As the comments stated remove the dcell = and make the function return a String instead of a Long. and you were missing a " in the third line.

Function AccountType(dCell As Variant) As String
    If Left(dCell, 4) = "0321" Then
        AccountType = "12 - ABC type"
    ElseIf Left(dCell, 3) = "021" Then
        AccountType = "543 - XYZ type"
    Else
        AccountType = "Logic Missing"
    End If
End Function
查看更多
登录 后发表回答