简体   繁体   中英

Unable to paste special from clipboard

I've been trying to complete a code which requires me to paste data from a Ctrl+A/C'd webpage into a sheet. This is what I've done so far.

Sub testPaste()

Sheets("sheet2").Delete
Sheets.Add.Name = "Sheet2"
Application.DisplayAlerts = False

Dim wscel As Range
Set wscel = Sheets("sheet2").Range("A1")

Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard

strPaste = DataObj.GetText

wscel.PasteSpecial (xlPasteValues)

End Sub

The code however gives me an error 1004. Removing (xlPasteValues) fixes it but it pastes the data as is which I can't work with. what is it that I'm doing wrong here?

You are using Range.PasteSpecial , which applies to pasting Excel Formatted Range Objects - documentation found here . Unfortunately, what sits on your Clipboard isn't an Excel Range Object, so you need the Worksheet.PasteSpecial method instead.

If your clipboard contains, for example an HTML sample, then you can "Paste As Text" like so:

    wscel.Worksheet.Activate
    wscel.Activate
    wscel.Worksheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= False

And, yes - this is one of the few cases were you can't avoid using Select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM