Join the text in cells with separators

nando88

Board Regular
Joined
Jun 22, 2013
Messages
124
I wish to make a macro in vba, to join the text in a selected range, substitute all the spaces with %20, and add |, after every cell. I wish to have this result in the cell next to the last cell with data in the row I am working in, since I am only going to be selecting cells in a row.
I have typed some code, but it doesn't seem to work, can someone please help me?

Code:
Function wMerge(Rng As Range)
Dim col1 As Integer
Dim col2 As Integer
Dim total As Integer
Dim subs As String
Dim concat1 As String
Dim concat2 As String
Dim s As String

part1 = Left(Rng.Address, Application.WorksheetFunction.Find("-", Rng.Address) - 1)
part2 = Right(Rng.Address, Len(Rng.Address) - Application.WorksheetFunction.Find("-", Rng.Address))
col1 = Range(Left(part1, 1)).Column
col2 = Range(part2).Column
s = Range(part2).Address(False, False)
    ColumnName = Left(s, Len(s) - Len(Format(Rng.Row, "0")))
For i = 1 To total
    concat1 = Replace(ActiveSheet.Cells(Range(part1).Row, total).Value, " ", "%20")
    concat2 = concat1 & "|"
Next i
ActiveSheet.Cells(total, Range(part2).Row).Value (concat2)
End Function
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
nando88,

You have coded for a function which I will assume you will use in the appropriate last cell?

If so then is this what you are wanting....
11

*ABCD
1StuffMore StuffEnough StuffStuff|More%20Stuff|Enough%20Stuff|

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:115px;"><col style="width:115px;"><col style="width:115px;"><col style="width:451px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
D1=TestMerge(A1:C1)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


Code:
Function TestMerge(Rng As Range)


For Each cell In Rng
Strg = Strg & WorksheetFunction.Substitute(cell.Value, " ", "%20") & "|"


Next cell


TestMerge = Strg
End Function

Hope that helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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