Excel VBA question - if ElseIf statement

I have a VBA conditional function that I hacked (I'm noob), which checks the name in the cell and then returns the appropriate options if one of the conditions is true, otherwise it returns an empty "". Instead of returning a space, I would like it to return the default cell value.

As an example, I have the following cells and results based on my function:

   Cells
   A        B
1  Bob      Bob Rob Robert
2  Mike     Mike Michael
3  Dan      Dan Daniel
4  Scott  

I would like the result for B4 to return the default value in A4 format (Scott), and not empty:

   Cells
   A        B
1  Bob      Bob Rob Robert
2  Mike     Mike Michael
3  Dan      Dan Daniel
4  Scott    Scott

Any help would be appreciated:

Here is my function (shortened version without all the names included in ElseIf):

Function NameList(pVal As String) As String

    If pVal = "Bob" Then
        NameList = "Bob Rob Robert"
    ElseIf pVal = "Mike" Then
        NameList = "Mike Michael"
    ElseIf pVal = "Dan" Then
        NameList = "Dan Daniel"
    Else
        NameList = ""
    End If

End Function

Thank!

+3
source share
3 answers

, Else
NameList = pVal

.

+5

else:

 [...]
 Else
     NameList = ""
 End If

(""), if/elseif .

pVal="Scott", . , ?

+2

I don’t know if I understand your question correctly, but try this

Function NameList(pVal As String) As String

If pVal = "Bob" Then
    NameList = "Bob Rob Robert"
ElseIf pVal = "Mike" Then
    NameList = "Mike Michael"
ElseIf pVal = "Dan" Then
    NameList = "Dan Daniel"
Else
    NameList = pVal
End If

End Function
+1
source

All Articles