【问题标题】:Paste without format within workbook and from other applications在工作簿中和从其他应用程序中无格式粘贴
【发布时间】:2020-07-06 15:27:58
【问题描述】:

我已经阅读了各种关于使用 VBA 防止由于用户粘贴数据而导致电子表格格式更改的帖子。

我不喜欢将 CTRL+V 绑定到宏的方法,因为用户可能不使用快捷方式,而且有些示例具有粘贴到随后单击的任何单元格的效果。

我在https://www.mrexcel.com/board/threads/vba-for-pastespecial-values-only.355553/ 中使用撤消功能的代码取得了最大的成功。这适用于工作簿中的复制和粘贴(虽然有点慢),也适用于单个或多个单元格,但不适用于 Excel 的其他实例或其他程序。

我发现 Excel vba paste special method fails whenever i try to paste 可以将粘贴到单个单元格中。

有没有办法将两者结合起来,达到防止从任何来源粘贴更改单元格格式的目的?

【问题讨论】:

  • 我的方法是让代码重置/更新由任何类型的单元格更改触发的格式、验证等。有时,有一些隐藏的模板范围是最快的,您只需将特殊格式复制并粘贴到用户刚刚粘贴到的范围中。
  • 有趣,完全不同的方法。我喜欢。你有一些示例代码吗?您是否将宏限制为仅在更改某些单元格时才运行?我可以将隐藏的模板单元格锁定,将它们的格式复制到粘贴的单元格中,然后将粘贴区域的样式再次更改为解锁样式。
  • 非常感谢任何来自这种方法的示例。
  • 您将不可避免地不得不更改任何代码并使其适应您的应用程序。一个好的开始是写下(在一张纸上)您要为所有不同范围保留的格式列表 -> 启动宏记录器并逐个应用这些格式。然后,您可以使用生成的代码使其更具动态性,并删除所有那些Select 语句。一旦您对您的代码感到满意,您就可以在Worksheet_Change 事件处理程序中运行它。如果您在此过程中遇到问题,请返回您生成/改编的代码,人们会尽力提供帮助

标签: excel vba copy-paste paste


【解决方案1】:

我认为我写了一些有用的东西,似乎对我很有效。

我没有实施您的建议,因为它看起来相当复杂,我发现另一个论坛帖子的“撤消”脚本形式略有不同,适用于复制和粘贴文本或单个单元格,或者范围从在 excel 实例中或来自外部程序。我已经将它与我发现的更常见的版本相结合,它也处理“自动填充”事件,并且我添加了一些其他的小增强功能。

我已确认我也使用过的其他论坛帖子。这是以防万一它对其他人有用。我不是专家,我希望代码可以改进,但它可以满足我的需要。

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

    'Prevents user changing cell formatting by undoing the paste and pasting the value only without formatting
    'Macro restricted to run on paste events to prevent the pasting of formats into the spreasheet, but not run on delete or type events
    'Works with ranges and individual cells, both within the workbook Excel instance and from other instances or external programs
    'A combination of:
    'https://www.mrexcel.com/board/threads/force-paste-special-values.230718/
    'https://stackoverflow.com/questions/45135039/excel-vba-code-to-force-values-only-paste-causes-strange-behavior-when-pasting-o
    'And:
    'https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-you-lock-formats-but-allow-data-entry/afccc43e-e191-417f-826c-d10a464a1b9a?page=4
    'A disadvantage of many macros, including this one, is that the undo history is cleared
    'Macro can be disabled for spreadsheet developement by changing the Devel value used in the first IF statement of the macro

    Dim Devel As Boolean
    Devel = "False" 'Remember to set back to False after spreadsheet development work
    If Devel = "False" Then
        'In normal user mode, prevent pastes etc from changing formatting
        Dim SavedVal As Variant
        Dim UndoString As String
        Dim srce As Range
        
        On Error GoTo ErrHan
        
        'Detect 'Paste' and 'Auto Fill' events in the undo list and only continue macro if these are found
        UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1) 'This always gives an error on spreadsheet open as the undo list is empty, but the error handling deals with it
        If Left(UndoString, 5) <> "Paste" And UndoString <> "Auto Fill" Then
            Exit Sub
        End If
    
        'Save the pasted value for later. This form and the use of type Variant is important in the flexibility of this macro
        SavedVal = Target.Value
    
        'Switch off events to prevent infinite loop
        Application.EnableEvents = False
        
        'Switch off screen updates
        Application.ScreenUpdating = False
    
        'Undo the user's paste
        Application.Undo
        
        'Handle 'Auto Fill' events differently. These can use the Target.PasteSpecial approach
        If UndoString = "Auto Fill" Then
            Set srce = Selection
            srce.Copy
            Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Application.SendKeys "{ESC}"
            Union(Target, srce).Select
        Else
            'Set target value. This form works with all selected data which the Target.PasteSpecial approaches don't
            Target.Value = SavedVal
        End If
    Else
        'In Devel mode so the developer can unlock the spreadsheet and change formats when pasting
    End If

ErrExit:
    'Remember to re-enable events
    Application.EnableEvents = True
    
    'Re-enable screen updates
    Application.ScreenUpdating = True
    Exit Sub

ErrHan:
    Resume ErrExit
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多