Excel VBA: Code not right justifying text or wrapping text

gertie193

Board Regular
Joined
Feb 28, 2008
Messages
82
Hello,

I modified a code by Chandoo where the user can select text in multiple cells in a column and the code will add it to the clipboard, join the text in a cell that the user pastes to, separated by a comma.

I can't get the text to right justify or wrap in the cell after I paste the text.

Can someone help? Using Excel 2016.

Thank you in advance.

Code:
Sub JoinAndMerge()'joins all the content in selected cells
'and puts the resulting text in clipboard to paste elsewhere
'then merges all cells
'https://chandoo.org/wp/merge-cells-without-loosing-data/


Dim outputText As String
Dim obj As New DataObject


Const delim = ", "


On Error Resume Next


For Each cell In Selection
    outputText = outputText & cell.Value & delim
Next cell


With Selection
    '.Clear
    outputText = Left(outputText, Len(outputText) - 2) 'removes last comma and space
    obj.SetText outputText 
    obj.PutInClipboard 'copy to clipboard
    .Merge
    .HorizontalAlignment = xlRight '<===HERE
    .VerticalAlignment = xlBottom
    .WrapText = True '<===HERE
End With


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
have you tried doing the selection a second time, when you have a complete output, I'm not sure you can add the formatting on the fly

maybe force a paste of the selection to a known cell, then apply the formatting, then copy to clipboard
 
Upvote 0
Thank you. I think I'm just going to leave it and format it manually.

God bless you.

gertie
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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