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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

byundtMechanical EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
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
janthonynAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.