Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA question - If then ElseIf statement

Tags:

excel

vba

I have a VBA conditional function I hacked together (I'm a noob) that checks for a name in a cell and then returns appropriate variations if one of the conditions is true, otherwise it returns a blank "". Instead of returning a blank, I'd 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'd like the result for B4 to return the default value in A4 (Scott), rather then a blank, like this:

   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's my function (abbreviated version without all 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

Thanks!

like image 282
Simon Avatar asked Dec 21 '22 16:12

Simon


1 Answers

I think
Else
NameList = pVal

solves your problem.

like image 64
Hossein Avatar answered Jan 03 '23 12:01

Hossein