简体   繁体   English

VBA Excel-保留目标单元格格式

[英]vba excel - preserve destination cell formatting

I am relatively new to vba, so please be gentle :) 我是vba的新手,所以请保持柔和:)

I have reviewed various scripts which supposedly preserve the formatting of cells on a spreadsheet when ctrl c/ctrl v or copy & paste is being used. 我查看了各种脚本,这些脚本应该在使用ctrl c / ctrl v或复制和粘贴时保留电子表格中单元格的格式。 Unfortunately I cannot seem to get any of the variations to work for my intentions. 不幸的是,我似乎无法获得符合我的意图的任何变化。 I suppose this might be due to the fact that a lot of the data being copy & pasted is being copied from other programs and pasted into the worksheet(therefore copying and keeping the formatting of the program from which it came). 我想这可能是由于以下事实:要复制和粘贴的许多数据是从其他程序复制并粘贴到工作表中的(因此复制并保留了它所来自的程序的格式)。 All of the macros I've tried to work with all seem to attempt to preserve formatting when copying between cells/worksheets or workbooks and doesn't address the data format when copying from another program. 我尝试使用的所有宏似乎都试图在单元格/工作表或工作簿之间进行复制时保留格式,而从另一个程序进行复制时却无法解决数据格式。

I'm looking for an alternate approach. 我正在寻找一种替代方法。 From a logical standpoint, I'm thinking that there should be a way on ctrl v or paste event, to have the copied data stored as a variable, stripped of its formatting and to only paste the raw value. 从逻辑的角度来看,我认为在ctrl v或粘贴事件上应该有一种方法,可以将复制的数据存储为变量,删除其格式并仅粘贴原始值。 I've tried playing around with pastespecial, but I'm not sure how to force a pastespecial (or replace paste with pastespecial). 我曾尝试过使用pastespecial,但不确定如何强制使用pastespecial(或将paste替换为pastespecial)。

Here is some code sample, but it doesn't seem to work for me. 这是一些代码示例,但是它似乎不适用于我。 I keep getting: 我不断得到:

cannot run the macro "C:...Test.xlsm'!MyPaste'. The macro may not be available in this workbook or all macros may be disabled 无法运行宏“ C:... Test.xlsm'!MyPaste'。此工作簿中的宏可能不可用,或者可能禁用了所有宏

Macros are definitely enabled and the code is pasted into [ThisWorkbook(Code)] 确实启用了宏,并将代码粘贴到[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

The reason for the error message is that your code is an event handler 出现错误信息的原因是您的代码是事件处理程序

see: 看到:

and

basically the Worksheet.Change Event (Excel) is fired when the user changes a cell in the worksheet. 当用户更改工作表中的单元格时,基本上会触发Worksheet.Change事件(Excel) Excel passes in the Worksheet Object object as sh and the Range Object (Excel) as Target . Excel将工作表对象对象传递为sh ,将范围对象(Excel)传递为Target Your code then uses these objects ( Ozgrid Excel VBA Crash Course Lesson 4 - Common objects ). 然后,您的代码将使用这些对象Ozgrid Excel VBA崩溃课程第4课 - 通用对象 )。

as David Zemens has suggested, you need to use the PasteSpecial method of the Sheet object. 正如David Zemens所建议的那样,您需要使用Sheet对象的PasteSpecial方法。 for further info, see MSDN libray: PasteSpecial Method [Excel 2003 VBA Language Reference] . 有关更多信息,请参见MSDN libray:PasteSpecial方法[Excel 2003 VBA语言参考]

and when you have finished all that reading, you will be ready to copy paste my code below: 阅读完所有内容后,您就可以在下面复制粘贴我的代码了:

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

so, bada bing bada bing , you have your working code, and some reading that should help you to understand better what your code is doing and how it does it. 因此, bada bing bada bing ,您拥有了工作的代码,并且阅读了一些书,应该可以帮助您更好地了解代码在做什么以及代码是如何工作的。

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

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