vba excel - 保留目标单元格格式

时间:2013-05-17 17:14:06

标签: excel-vba vba excel

我对vba比较新,所以请保持温柔:)

我已经查看了各种脚本,当ctrl c / ctrl v或copy& amp;时,它们可以保留电子表格中单元格的格式。正在使用粘贴。不幸的是,我似乎无法让任何变化符合我的意图。我想这可能是因为很多数据都是复制和放大的。粘贴正在从其他程序复制并粘贴到工作表中(因此复制并保留程序的格式化)。我尝试使用的所有宏似乎都试图在单元/工作表或工作簿之间复制时保留格式,并且在从其他程序复制时不解决数据格式。

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

1 个答案:

答案 0 :(得分:1)

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

请参阅:

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

David Zemens所示,您需要使用工作表对象的 PasteSpecial 方法。有关详细信息,请参阅MSDN libray: 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,你有自己的工作代码,还有一些阅读材料可以帮助你更好地理解你的代码正在做什么以及它是如何做到的。