VBA function saved in personal.xls project returns #NAME? error

I added code for a worksheet function and saved it in a module in the PERSONAL.xls VB project. When I use this function in a worksheet formula, I'm supposed to be able to reference the values in a range of cells and return a concatenated text with the repeated separators entered into the second parameter of the function.
Function Join(RangeToJoin As Range, BetweenEach)

    Join = " "
    For Each cell In RangeToJoin
        Join = Join & cell.Value & BetweenEach
    Next cell
    
    'Get rid of the final Between
    If Len(Join) > 0 Then
        Join = Left(Join, Len(Join) - Len(BetweenEach))
End Function

Open in new window

janthonynAsked:
Who is Participating?
 
byundtConnect With a Mentor Commented:
Did you use the fx icon to the left of the formula bar to enter your function in the formula? If so, you might have ended up with a formula like this:
=Personal.xls!Join(C2:E2," ")

The above formula works in my Excel 2003 test workbook.

Note that I needed to change your code to this:
Function Join(RangeToJoin As Range, BetweenEach)

    Join = " "
    For Each cell In RangeToJoin
        Join = Join & cell.Value & BetweenEach
    Next cell
    
    'Get rid of the final Between
    If Len(Join) > 0 Then
        Join = Left(Join, Len(Join) - Len(BetweenEach))
    End If          'Added statement
End Function

Open in new window


Brad
0
 
Patrick MatthewsConnect With a Mentor Commented:
janthonyn,

Brad helped you with your immediate problem.  Going forward, if you do not want to have to qualify the function name with the reference to your personal macro workbook, you could put that code into an add-in file.

Either way, you will potentially run into trouble if you ever have to give someone else a workbook using that function.  For that reason, I tend not to deploy UDFs in a personal macro workbook or in an add-in.  Instead, I've developed an add-in that will take functions from my personal library and add them into whatever workbook needs them.  That makes the code fully portable.

It does, of course, complicate things if I ever want to do code updates :(

Patrick
0
 
janthonynConnect With a Mentor Author Commented:
I added the End if to the code and used Personal.xls!Join to call it on the spreadsheet and got another error: Compile error: Variable not defined. cell in For Each cell in RangeToJoin is highlighted with the error. When I added Dim cell as Range, the function worked, but only if I entered it as Personal.xls!Join
0
 
aikimarkCommented:
Also, JOIN() is the name of an intrinsic VBA function, partnered with the SPLIT() function.  You should avoid naming functions and variables that are the same as your (VB) language items.
0
 
janthonynAuthor Commented:
I added the variable definition which was missing in the first iteration of my code
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.