VBA: Paste in a single cell NOT multiple rows

pivotpp

New Member
Joined
Jun 23, 2017
Messages
12
Hi all,
This is a seemingly simple operations but can't figure it out.
Goal: I have some content copied, I want to paste it into a single cell.
The default when I control+paste into excel is that is paste it as multiple rows/lines.
Even when I right click for more paste options, there isn't any, I only see the paste as text which is the above, paste into several rows.
THE ONLY WAY, I can do is is by double-clicking into the cell and then control-paste OR press F2 (which is the same as double-clicking into a cell) and pasting.
Any others ways?
Since this double-clicking/F2 method is the only one I know...I'm having trouble replicating this as VBA code.
Again, ultimately I just want a VBA script to paste my content into a single cell.
Any help/input much appreciated. Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi pivotpp,

You can use the following code to replicate F2 & Ctrl+V functionality (so that it pastes content into a single cell):

Code:
Public Sub PasteContent()
    Dim dob         As MSForms.DataObject
    
    Set dob = New MSForms.DataObject
    
    With dob
        Call .GetFromClipboard
        ActiveCell.Value = Replace$(.GetText, vbTab, " ")
    End With
    
    Set dob = Nothing
End Sub

You need to make sure that you have Object Library checked in References window:
VBA window > Tools > References > "Microsoft Forms 2.0 Object Library"

Let me know if it works for you.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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