繁体   English   中英

我正在尝试使用 excel vba 将剪切和粘贴更改为复制粘贴和删除

[英]I am trying to change cut and paste to copy and paste and delete using excel vba

我找到了这段代码,它似乎几乎是我需要的。 有没有办法让它复制粘贴并删除源而不是只显示一个消息框? 我使用的工作表具有指向另一个工作表的链接,如果我使用剪切和粘贴,则会出现 #REF 错误。

谢谢

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)


Public NewRange As String

Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut

MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select

End Sub

通过使用.PasteSpecial Paste:=xlPasteValues ,您将粘贴值而不是公式,因此对其他工作表的引用将不存在。 这意味着您不必删除对其他工作表的任何引用。

为了使其正常工作,只需更新Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues的“Sheet2”和“A1”引用

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
  
  Public NewRange As String

  Select Case Application.CutCopyMode
  Case Is = False
    'do nothing
  Case Is = xlCopy
    'do nothing
  Case Is = xlCut
    MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
    Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False 'clear clipboard and cancel cut
  End Select

End Sub

暂无
暂无

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

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