首页 > 解决方案 > 如何在 Excel VBA/宏中处理 CTRL-V?

问题描述

我有一个 VBA 宏,每次将单个值输入到 A 列(Workbook_SheetChange)的单元格中时,它都会启动。

一些用户将多个单元格或一整行或多行整行粘贴到工作表中以使其更容易(CTRL-V)。

如果粘贴整行/行,则宏会失败,有时整个 VBA_engine 会损坏,用户需要重新启动 Excel。

我想以在按下 CTRL-V 之后但在将内容粘贴到单元格之前禁用 VBA 代码的方式处理粘贴(CTRL-V)。之后应启动 VBA 代码。

我怎样才能做到这一点?

标签: excelvba

解决方案


问题是编写Worksheet_Change事件的人没有让它处理来自多个单元格的更改。Either rewrite it or make sure to exit the event, whenever more than one cell is selected:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub

    'rest of the code here

End Sub

推荐阅读