Concatenate Custom Function

AJMTL

New Member
Joined
Mar 9, 2013
Messages
8
Hi guys,

I need to concatenate the content of 3 cells using the separator "-" but only when cells are non empty

The custom function I'm trying to create would look like this :

Code:
Function ConcatWithSep(Sep As String, Str1 As String, Str2 As String, Str3 As String) As String
...
End Function
The function would be used like this :

=ConcatWithSep("-",B2,C2,D2)

The function would produce the following results :

Case #
String 1
String 2
String 3
FunctionResult
ResultString lenght
1
X
Y
Z
X-Y-Z
5
2
X
Y

X-Y
3
3
X

Z
X-Z
3
4

Y
Z
Y-Z
3
5
X


X
1
6

Y

Y
1
7


Z
Z
1
8





<tbody> </tbody>


Case #
String 1
String 2
String 3
Function Result
Result String lenght
1
X
Y
Z
X-Y-Z
5
2
X
Y

X-Y
3
3
X

Z
X-Z
3
4

Y
Z
Y-Z
3
5
X


X
1
6

Y

Y
1
7


Z
Z
1
8




0

<tbody>
</tbody>

I would very much appreciate any and all help and insights into this problem.

Many thanks,

Alex
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
AJMTL,

Maybe this....

Code:
Function ConcatWithSep(Sep As String, Str1 As String, Str2 As String, Str3 As String)
cws = Str1 & Sep & Str2 & Sep & Str3
For c = 1 To 2
If Right(cws, 1) = Sep Then cws = Left(cws, Len(cws) - 1)
If Left(cws, 1) = Sep Then cws = Right(cws, Len(cws) - 1)
Next c
ConcatWithSep = cws
End Function

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,548
Members
449,170
Latest member
Gkiller

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top