【问题标题】:Excel Clipboard ClearExcel剪贴板清除
【发布时间】:2016-08-03 19:22:07
【问题描述】:

我是编写 excel 宏的新手,但我正在努力完成工作。无论如何,我正在尝试将数据从 2 个工作表导出到另一个工作簿中的另外 2 个工作表,但是当我到达清除剪贴板的地步时,它根本不起作用 :( 任何人都可以帮助我吗?这是我的代码:

Sub manufacturer_export()

Dim Return_Shipment_Template_Proba As Workbook
Dim wbTarget As Workbook
Dim activeSht As Worksheet

    Set Return_Shipment_Template_Proba = ThisWorkbook
    Set wbTarget = Workbooks.Open("file:///F:\FEM Backup\20.07.2016\08_GT Returns & Sample tracking\01_Returns Summary\Returns Summary.xlsm")

Set activeSht = Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp")
    With Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp").Range("A3:X3")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
        If .Cells(1, 24).Value = "" Then
    'Nothing in this column.
    'Do nothing.
    Else
    Return_Shipment_Template_Proba.Sheets("GT_GU10_Lamp").Range(.Cells(1, 24), .End(xlDown)).Copy
    wbTarget.Sheets("GT_GU10_Lamp").Activate
    ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=True, Transpose:=False
End If
End With

Application.CutCopyMode = False

Set activeSht = Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp")
With       Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp").Range("A3:X3")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
    If .Cells(1, 24).Value = "" Then
    'Nothing in this column.
    'Do nothing.
        Else
    Return_Shipment_Template_Proba.Sheets("GT_COB_GU10_Lamp").Range(.Cells(1, 24), .End(xlDown)).Copy
    wbTarget.Sheets("GT_COB_GU10_Lamp").Activate
    ActiveCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=True, Transpose:=False
End If

End With

wbTarget.Save

End Sub

【问题讨论】:

  • “根本行不通”到底是什么意思?某处有错误吗?我也不确定您所说的“清除剪贴板”是什么意思。你指的是Return_Shipment_Template.Application.CutCopyMode = False吗?
  • 代替Return_Shipment_Template.Application.CutCopyMode = False试试Application.CutCopyMode = False
  • 当我说它不起作用时,我的意思是它将相同的数据从第一个源工作表复制到两个目标工作表。
  • 您是否尝试过使用 Worksheet 变量,例如 Dim activeSht As Worksheet Set activeSht = Return_Shipment_Template.Sheets("GT_GU10_Lamp") With activeSht.Range("A3:X3") ... End With 并重复第二张工作表
  • Range(.Cells(1, 24), .End(xlDown)).Copy ,这行很麻烦:第一个范围没有被点“。”引用并且单元格是......所以它当然会从活动工作表中复制。

标签: excel vba


【解决方案1】:

使用 API 调用的替代方法,您可以使用它来清除 Windows 剪贴板:

Private Sub ClearClipboard()
    CreateObject("WScript.Shell").Run "CMD /C @ECHO OFF | CLIP", 0, False
End Sub

'// Example

Sub Foo()

'// Do something here...
ClearClipboard
'// clipboard is now empty

End Sub

【讨论】:

  • 似乎没有清除剪贴板。即使运行此命令后,我仍然可以在剪贴板视图中看到堆积的东西
【解决方案2】:

这是使用 API 清除剪贴板的 64 位代码:

坦率地说,您的错误不在剪贴板中(请参阅我的 cmets),但这里是:

Option Explicit

'Open the clipboard to read
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long

'clear clipboard
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long

'Close the clipboard
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long




Sub Clear_Clipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
Application.CutCopyMode = False
End Sub

【讨论】:

    猜你喜欢
    • 2013-08-26
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2015-12-20
    相关资源
    最近更新 更多