简体   繁体   English

Excel VBA - 从剪贴板粘贴文本(错误 1004)

[英]Excel VBA - paste text from clipboard (error 1004)

can someone help me with this code?有人可以帮助我使用此代码吗?

Sub TEST()
Dim Val As Variant
Sheets("Sheet 3").Select
Val = Range("A2").Value
Sheets("Sheet 1").Select
Range("AY" & Val).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Sheet 3").Select
Application.CutCopyMode = False
End Sub

I have to transfer simple number from web to excel.我必须将简单的数字从网络传输到 excel。 I need to copy number from web into a clipboard, then go to excel and run Macro.我需要将数字从网络复制到剪贴板,然后转到 excel 并运行宏。 This macro should go to "sheet 3" set "Val" based on A2 value, go to sheet 1, select range in AY & "Val" and paste to this cell data (the number) from clipboard.这个宏应该根据 A2 值转到“工作表 3”设置“Val”,转到工作表 1,在 AY 和“Val”中选择范围并从剪贴板粘贴到该单元格数据(数字)。

But when macro reach line 7 (Selection.PasteSpecial) Im getting Error:但是当宏到达第 7 行(Selection.PasteSpecial)时,我收到错误:

Run-time error '1004': PasteSpecial method of Range class failed运行时错误“1004”:Range 类的 PasteSpecial 方法失败

Where I have the bug, please :)请问哪里有BUG:)

You must use a MSForms.DataObject to interact with the clipboard:您必须使用 MSForms.DataObject 与剪贴板交互:

Sub TextFromClipboard()
'This works only with text!
  Dim oData As Object

  'New MSForms.DataObject with guid and late binding
  Set oData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

  'Get text from clipboard
  'to the DataObject
  oData.GetFromClipboard

  'Show text
  MsgBox oData.GetText
End Sub

To put text to clipboard you can use the following 2 methods:要将文本放入剪贴板,您可以使用以下两种方法:

oData.SetText sText
oData.PutInClipboard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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