Paste from clipboard to Excel

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
I want a macro to paste plain text into the current cell.
What I recorded was:

Code:
Sub PasteUnformattedText()
'
' PasteUnformattedText Macro
' Keyboard Shortcut: Ctrl+Shift+V

Selection.PasteSpecial Paste:=xlPasteValues

End Sub

This works fine when the clipboard contains a value from Excel.
But if the clipboard has formatted text from Word, let's say, it fails. What I want to paste is the CF_TEXT format from the Windows clipboard.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This worked for me

VBA Code:
ActiveSheet.PasteSpecial Format:="Text"
 
Upvote 0
If I copy (CTRL+C) from Excel to the clipboard and incorporate that code into a macro, I get PasteSpecial method of Worksheet class failed.
I want the clipboard content, regardless of how it got there, copied into the active cell when I run the macro.
There seems to be a difference if you CTRL+C from Excel versus from Word, for example.

This generated the error:

Code:
Sub ToExcel()
ActiveSheet.PasteSpecial Format:="Text"
End Sub

Which probably is the case since there's no target for the paste, I suspect. But, I also think it has something to do with CTRL+C from Excel vs Word, as I said.
 
Upvote 0
I believe Paste1 will include the formatting.
N.B. There may be better solutions; I do not use VBA very much.

Sub Paste1()
ActiveSheet.Paste
End Sub
Sub Paste2()
ActiveSheet.PasteSpecial Format:="Text"
End Sub
Sub Paste3()
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
End Sub
 
Upvote 0
Try the following:

I am guessing that you would want to paste via a command button?

1. Create a command button on the worksheet where you would like the paste to happen.
2. Put code below in the command button code. Right click button and view code.

VBA Code:
Private Sub CommandButton1_Click()
    Dim dFname As DataObject
    Dim strText As String
    On Error Resume Next
    Set dFname = New DataObject
    dFname.GetFromClipboard
    strText = dFname.GetText
    If Trim(strText) = "" Then
        MsgBox "Clipboard is empty...Please select text from source", vbOKOnly, "Message"
    Else
        ActiveSheet.Paste = strText
    End If
End Sub

3. Copy something from word and then select any cell ypou want in excel and then just click the button.

Let me know if this works for you.
 
Upvote 0
The code in post 5 @Jimmypop requires reference to Microsoft Forms Object Library be selected (in VBA editor under Tools \ References)

Annoyingly it is sometimes not listed under available references
- if so hit "Browse" button and you should find "FM20.dll" (version 2.0) in Windows\System32 (hopefully :unsure: )
- another way is to insert a userform and a reference to Microsoft Forms Object Library is automatically selected
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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