vba - Application.OnKey 无法正确重置

标签 vba excel

我正在尝试帮助此论坛中的另一位用户 ( reference )。目标是使用数字键盘上的 Enter 键来触发宏,而不是使用 Worksheet_Change 事件宏来实现相同目的。

在运行任何操作之前,都 < kbd>Enter 键的工作方式相同。您输入内容,触摸任一键, Material 就会放置在单元格中,并且选择向下移动。如果您只按下任一键,Selection 只会向下移动。

在我放置的标准模块中:

Sub NumericEnter()
    Application.OnKey "{ENTER}", "InsertIntoTables"
End Sub

Sub InsertIntoTables()
    MsgBox "Inserting"
End Sub

运行 NumericEnter() 后,每次触摸数字键盘上的 Enter 键时都会调用正确的宏。正常的 Enter 键不受影响。

然后我尝试通过运行以下命令来恢复数字 Enter 键:

Sub ClearEnter()
    Application.OnKey "{ENTER}", ""
End Sub

数字Enter键部分恢复到其原始行为。它不再调用宏;它允许将数据输入到单元格中,但它根本不会移动选择!(常规回车键仍然正常工作)

我不知道我错过了什么。

我看过的内容:

  • 普通的 Enter 键、箭头键和 TAB 键可以很好地更改选择
  • 鼠标点击可以很好地更改选择
  • Application.MoveAfterReturnApplication.MoveAfterReturnDirection 的值看起来也不错。

最佳答案

仅使用

Application.OnKey "{ENTER}" 

没有第二个参数似乎会清除宏映射并返回默认键操作(Excel 2013)

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-onkey-method-excel

expression.OnKey( Key , Procedure )

Procedure 参数的描述如下:

A string indicating the name of the procedure to be run. If Procedure is "" (empty text), nothing happens when Key is pressed. This form of OnKey changes the normal result of keystrokes in Microsoft Excel. If Procedure is omitted, Key reverts to its normal result in Microsoft Excel, and any special key assignments made with previous OnKey methods are cleared.

关于vba - Application.OnKey 无法正确重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552588/

相关文章:

Excel:从日期字符串中提取数字

sql - 以编程方式存档 Access 后端表

excel - 我想通过 VBA 在我的工作表中创建一系列协方差矩阵

excel - 如何在不索引的情况下删除突出显示的单元格?

vba - Excel 按首字母过滤列中超过 2 个值

c# - 如何使用 Spreadsheet Gear C# 将 IRange 中所有日期格式的单元格转换为 DateTime?

python - 自动与excel中的按钮/宏交互

vba - 读取 Excel 文件的 VBA 宏(或 vbaProject.bin),而无需在 MS Excel 中打开它

c# - 如何在文档级加载项中将 C# 类公开给 VBA 模块?

vba - 从不同的工作表复制行