Excel VBA 如何使 Enter 键的行为类似于 Tab 键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:08:51  来源:igfitidea点击:

Excel VBA how do I make the Enter key behave like the Tab key

excel-vbavbaexcel

提问by Symon

I'd like the enter key to function just like the tab key does when using the worksheet to enter values. I'm not sure if that's enough information for you to help me so just ask questions if it's not.

我希望回车键的功能就像使用工作表输入值时的 Tab 键一样。我不确定这些信息是否足以帮助您,所以如果不是,请提出问题。

I was able to find a setting in Tools>Options>Edit Move selection after enter - right. I'd like that same functionality but only for the one specific worksheet.

输入后,我能够在工具>选项>编辑移动选择中找到一个设置 - 对。我想要相同的功能,但仅适用于一个特定的工作表。

回答by MikeD

use the Application.MoveAfterReturnDirectionproperty - best to set in a Workbook_Open().

使用Application.MoveAfterReturnDirection属性 - 最好设置在Workbook_Open().

e.g.

例如

Application.MoveAfterReturn = True  ' here we say thou shallst move
Application.MoveAfterReturnDirection = xlToRight  ' here we say move right

Hope that helps Good luck MikeD

希望有帮助 祝你好运 MikeD

edit:

编辑:

if you have more than 1 book open in the same application, you better use Workbook and Sheet Activate and Deactivate triggers as well to limit the effect to the narrowest possible

如果您在同一个应用程序中打开了 1 本书以上,则最好同时使用工作簿和工作表激活和停用触发器,以将效果限制在尽可能小的范围内

edit 2:

编辑2:

you can maybe save the original status in a trigger in the ThisWorkbookmodule

您可以将原始状态保存在ThisWorkbook模块的触发器中

Dim OldDirection As Long

Private Sub Workbook_Open()
    OldDirection = Application.MoveAfterReturnDirection
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.MoveAfterReturnDirection = OldDirection
End Sub

and rely on Sheet_Activateand Sheet_Deactivateto actually set the property to xlToRightand reset it to OldDirection(so whatever the user chose as standard behaviour). Test with multiple open workbooks, jump back and forth; these triggers not always do the obvious at obvious times ....

并依赖Sheet_ActivateSheet_Deactivate实际将属性设置为xlToRight并将其重置为OldDirection(因此无论用户选择作为标准行为)。用多个打开的工作簿进行测试,来回跳转;这些触发器并不总是在明显的时候做明显的事情......