簡體   English   中英

VBA Excel-保留目標單元格格式

[英]vba excel - preserve destination cell formatting

我是vba的新手,所以請保持柔和:)

我查看了各種腳本,這些腳本應該在使用ctrl c / ctrl v或復制和粘貼時保留電子表格中單元格的格式。 不幸的是,我似乎無法獲得符合我的意圖的任何變化。 我想這可能是由於以下事實:要復制和粘貼的許多數據是從其他程序復制並粘貼到工作表中的(因此復制並保留了它所來自的程序的格式)。 我嘗試使用的所有宏似乎都試圖在單元格/工作表或工作簿之間進行復制時保留格式,而從另一個程序進行復制時卻無法解決數據格式。

我正在尋找一種替代方法。 從邏輯的角度來看,我認為在ctrl v或粘貼事件上應該有一種方法,可以將復制的數據存儲為變量,刪除其格式並僅粘貼原始值。 我曾嘗試過使用pastespecial,但不確定如何強制使用pastespecial(或將paste替換為pastespecial)。

這是一些代碼示例,但是它似乎不適用於我。 我不斷得到:

無法運行宏“ 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

出現錯誤信息的原因是您的代碼是事件處理程序

看到:

當用戶更改工作表中的單元格時,基本上會觸發Worksheet.Change事件(Excel) Excel將工作表對象對象傳遞為sh ,將范圍對象(Excel)傳遞為Target 然后,您的代碼將使用這些對象Ozgrid Excel VBA崩潰課程第4課 - 通用對象 )。

正如David Zemens所建議的那樣,您需要使用Sheet對象的PasteSpecial方法。 有關更多信息,請參見MSDN libray:PasteSpecial方法[Excel 2003 VBA語言參考]

閱讀完所有內容后,您就可以在下面復制粘貼我的代碼了:

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 ,您擁有了工作的代碼,並且閱讀了一些書,應該可以幫助您更好地了解代碼在做什么以及代碼是如何工作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM