Merging text in a single cell from a range

rubic

Active Member
Joined
Jul 9, 2008
Messages
251
How can I merge into a single cell from a range of selected cells that contain text. If the range of cells do have empty cells it should be smart enough to skip over or not to create an additional space. Any idea how can this be done? Macro or formula?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I have tried using concatenate function but I need to create a space in between text from each cell so the text will not join together a single meaningless word.
 
Upvote 0
Hi, If you select your cells by holding Down "Ctrl" , to select non contiguious cell/ranges, then run the code it will put the complete string" in "A1".
Code:
Dim cl As Range, Txt As String
For Each cl In Selection 
If cl <> "" Then
Txt = Txt & cl & Chr(32)
End If
Next cl
Range("A1") = Txt
Regards Mick
 
Upvote 0
Strange, I needed the same thing yesterday.

I used the code below in the "right click event". I could drag over the range with the LMB. Right click combines to first cell in range and clears the rest.

It delimits the merged text with a semi-colon and a space.

Hope it helps.

Gary

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

Dim oRange As Range
Dim oCell As Range
Dim sTemp As String

Set oRange = Application.Selection

sTemp = ""
For Each oCell In oRange
If oCell.Value <> "" Then
sTemp = sTemp & oCell.Value & "; "
End If
Next

sTemp = Left(sTemp, (Len(sTemp) - 2))

oRange.Clear

oRange.Cells(1).Value = sTemp


End Sub
 
Upvote 0
i do this when working with first and last names in seperate columns:

=concatenate(A1," ",B1)

putting a space in quotes will place it between the two different text strings.
 
Upvote 0
Thank guys for your suggestion and help. I will try out to see which proposal suit my needs best.
 
Upvote 0
Gary Mcmaster,
About your codes. If I need the merged text to be in the active cell rather than cell A1 how will the codes be like?
 
Upvote 0
or rather to be on the first cell of the selected range of cells to merge the text.
 
Upvote 0
or rather to be on the first cell of the selected range of cells to merge the text.

That's what it does as I showed it. Select a range of cells, right click, and it will merge the text from the selected range into the first cell of that selected range.

At the end of this line:

sTemp = sTemp & oCell.Value & "; "

The "; " gets added between each piece of merged text. Change it to whatever you want.

Gary
 
Upvote 0
Gary,

Sorry I got the wrong guy regarding my question before. About youryour suggested codes. I put these codes is in the sheet itself. Right clicking did not seem to do anything. Did I put the codes in the wrong location?
 
Upvote 0

Forum statistics

Threads
1,214,542
Messages
6,120,116
Members
448,945
Latest member
Vmanchoppy

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