0

我对vba比较陌生,所以请温柔:)

我已经查看了各种脚本,这些脚本应该在使用 ctrl c/ctrl v 或复制和粘贴时保留电子表格上的单元格格式。不幸的是,我似乎无法让任何变化符合我的意图。我想这可能是由于许多正在复制和粘贴的数据是从其他程序复制并粘贴到工作表中(因此复制并保留它来自的程序的格式)。我尝试使用的所有宏似乎都试图在单元格/工作表或工作簿之间复制时保留格式,并且在从另一个程序复制时没有解决数据格式问题。

我正在寻找另一种方法。从逻辑的角度来看,我认为在 ctrl v 或粘贴事件上应该有一种方法,将复制的数据存储为变量,剥离其格式并仅粘贴原始值。我尝试过使用 pastespecial,但我不确定如何强制使用 pastespecial(或用 pastespecial 替换 paste)。

这是一些代码示例,但它似乎对我不起作用。我不断得到:

无法运行宏“C:...Test.xlsm'!MyPaste'。此工作簿中可能没有该宏,或者所有宏都可能被禁用

宏肯定启用,代码粘贴到 [ThisWorkbook(Code)]

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim UndoList As String

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    On Error GoTo Whoa

    '~~> Get the undo List to capture the last action performed by user
    UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)

    '~~> Check if the last action was not a paste nor an autofill
    If Left(UndoList, 5) <> "Paste" And UndoList <> "Auto Fill" Then GoTo LetsContinue

    '~~> Undo the paste that the user did but we are not clearing the clipboard
    '~~> so the copied data is still in memory
    Application.Undo

    If UndoList = "Auto Fill" Then Selection.Copy

    '~~> Do a pastespecial to preserve formats
    On Error Resume Next
    '~~> Handle text data copied from a website
    Target.Select
    ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False

    Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    On Error GoTo 0

    '~~> Retain selection of the pasted data
    Union(Target, Selection).Select

LetsContinue:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
4

1 回答 1

1

错误消息的原因是您的代码是事件处理程序

看:

基本上,当用户更改工作表中的单元格时,会触发Worksheet.Change 事件 (Excel) 。Excel 将Worksheet Object对象作为sh传入,将Range 对象 (Excel)作为Target传入。然后,您的代码将使用这些对象Ozgrid Excel VBA 速成课程第 4 课-常用对象)。

正如David Zemens所建议的,您需要使用Sheet对象的PasteSpecial方法。有关详细信息,请参阅MSDN 库:PasteSpecial Method [Excel 2003 VBA Language Reference]

当你完成所有阅读后,你就可以复制粘贴我的代码了:

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim UndoList As String

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    On Error GoTo Whoa

    '~~> Get the undo List to capture the last action performed by user
    UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)

    '~~> Check if the last action was not a paste nor an autofill
    If Left(UndoList, 5) <> "Paste" And UndoList <> "Auto Fill" Then GoTo LetsContinue

    '~~> Undo the paste that the user did but we are not clearing the clipboard
    '~~> so the copied data is still in memory
    Application.Undo

    If UndoList = "Auto Fill" Then Selection.Copy

    '~~> Do a pastespecial to preserve formats
    On Error Resume Next
    '~~> Handle text data copied from a website
    Target.PasteSpecial Paste:=xlPasteValues
    On Error GoTo 0

    '~~> Retain selection of the pasted data
    Target.Select

LetsContinue:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

所以,bada bing bada bing你有你的工作代码,和一些阅读应该可以帮助你更好地理解你的代码在做什么以及它是如何做的。

于 2013-05-18T15:52:23.467 回答