In the latest version of excel you would use Textjoin to dpo what you need.
I will write a user defined function to do the same thing for you.
Paste the code into a standard macro module and use the formula:-
Formula:
=Textjoin("-",True,Range)
to get your result.
I will programme the Textjoin function to work as described in this link:
https://support.office.com/en-gb/art...3-0e8fc845691c
It will take me a few minutes.
This is written to work on a horizontal range as you described.
Function Textjoin(dlim As String, flag As Boolean, range As range)
A = range.Value
For Count = 1 To UBound(A, 2)
Textjoin = Textjoin & A(1, Count)
If flag = False Or A(1, Count) <> "" Then Textjoin = Textjoin & dlim
Next
Textjoin = Left(Textjoin, Len(Textjoin) - Len(dlim))
End Function
NB not fully tested because I cannot use the function name Textjoin on my pc.
Tested thus:-
Function TJoin(dlim As String, flag As Boolean, range As range)
A = range.Value
For Count = 1 To UBound(A, 2)
TJoin = TJoin & A(1, Count)
If flag = False Or A(1, Count) <> "" Then TJoin = TJoin & dlim
Next
TJoin = Left(TJoin, Len(TJoin) - Len(dlim))
End Function
Ok
This version works for all types of range. Eg A1 to E1, A1 to A5 ans also A1 to E5.
Function TJoin(dlim As String, flag As Boolean, range As range)
A = range.Value
For Count1 = 1 To UBound(A)
For Count = 1 To UBound(A, 2)
TJoin = TJoin & A(Count1, Count)
If flag = False Or A(Count1, Count) <> "" Then TJoin = TJoin & dlim
Next
Next
TJoin = Left(TJoin, Len(TJoin) - Len(dlim))
End Function
Bookmarks