简体   繁体   English

excel vba回车键移动选择

[英]excel vba return key move selection

I have a question: in excel I want, when I press the return key, move the selection one cell to the right.我有一个问题:在我想要的 excel 中,当我按回车键时,将所选内容向右移动一个单元格。 I know this option can be changed under options in excel.我知道可以在 excel 中的选项下更改此选项。 I want to set this option programmatically so it only works with one workbook when it loads, but not with other workbooks... so the option will be resetted back to the original "down" action when the workbook is closed.我想以编程方式设置此选项,以便它在加载时仅适用于一个工作簿,而不适用于其他工作簿......因此当工作簿关闭时,该选项将重置回原始的“向下”操作。

Can this be done in vba?这可以在vba中完成吗? and how?如何?

While maybe not completely recommended – using TAB would be how most people work this – You can change the option with the MoveAfterReturnDirection property.虽然可能不完全推荐 - 使用 TAB 是大多数人的工作方式 - 您可以使用MoveAfterReturnDirection属性更改选项。

So if you would have something like所以如果你有类似的东西

Private Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)
    Application.MoveAfterReturnDirection = xlToRight
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)
    Application.MoveAfterReturnDirection = xlDown
End Sub

in the code for the workbook, that should accomplish something along the lines of what you are asking.在工作簿的代码中,应该按照您的要求完成一些事情。

Notice that this will change the setting for anyone who are using the workbook, and if they had anything else than Down set, then that will change.请注意,这将更改使用工作簿的任何人的设置,如果他们设置了 Down 以外的任何内容,则该设置将更改。 You could work around this by capturing the original value using a global variable, but I wouldn't bet my life on that it wouldn't mess up someones setting at some point.您可以通过使用全局变量捕获原始值来解决此问题,但我不会打赌它不会在某些时候弄乱某人的设置。
Actually, testing the latter managed to somehow set my settings to "left" , after closing the book.实际上,在关闭本书后,测试后者设法以某种方式将我的设置设置为 "left"

An even worse way to do it would be to completely change the function of the Enter key, using the OnKey method.更糟糕的方法是使用OnKey方法完全更改 Enter 键的功能。 Making the Enter key return a Tab key press.使 Enter 键返回 Tab 键按下。 The approach would be the same, but using OnKey and then creating a sub that uses something like SendKeys to return the Tab press.方法是相同的,但是使用OnKey然后创建一个使用类似SendKeys的子程序来返回 Tab 键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM