简体   繁体   English

VBA Excel - 用 Alt+ Enter 替换 Enter 键

[英]VBA Excel - Replace Enter key with Alt+ Enter

I am trying to replace Enter key with Alt+Enter so that I can write multiline in cells with ease.我正在尝试用Alt+Enter替换Enter键,以便我可以轻松地在单元格中编写多行。

I have seen that there is a function Application.OnKey and Application.SendKeys and I wanted to use those something like this:我已经看到有一个 function Application.OnKeyApplication.SendKeys我想使用这样的东西:

Application.OnKey "~" , Application.SendKeys("%~") 

But where do I place those?但是我应该把它们放在哪里? Or any other ideas?还是有其他想法?

I think I agree with @Andreas, this is unlikely to work using these methods.我想我同意@Andreas,这不太可能使用这些方法。

This is what I tried: I made a button Button1 and in its click method I assign the Enter key to send Alt-Enter as you suggest in the question:这就是我尝试的方法:我制作了一个按钮 Button1 并在其单击方法中分配 Enter 键以按照您在问题中的建议发送 Alt-Enter:

Sub Button1_onClick()

    Call Application.OnKey("~", "SendAltEnter")

End Sub

Sub SendAltEnter()

    Application.SendKeys ("%~")
    
End Sub

This does in fact re-route the Enter key, but apparently the Alt-Enter results in another call to the method for the "Enter" part of "Alt-Enter" -- it results in an infinite loop the first time you hit enter after having clicked the button, and you have to restart your Excel application to clean it up.这实际上会重新路由 Enter 键,但显然 Alt-Enter 会导致对“Alt-Enter”的“Enter”部分的方法的另一个调用——它会在您第一次按 Enter 时导致无限循环单击按钮后,您必须重新启动 Excel 应用程序以清理它。

I also tried this, simply using another key near Enter, namely # (at least on German keyboards) which you could hit instead of Alt-Enter:我也试过这个,只需在 Enter 附近使用另一个键,即 #(至少在德语键盘上),您可以按下它而不是 Alt-Enter:

Sub Button1_onClick()

    Call Application.OnKey("#", "SendAltEnter")

End Sub

Sub SendAltEnter()

    Application.SendKeys ("%~")
    
End Sub

The key '#' is intercepted, but not if you are in input mode in a cell, only if the focus is somewhere in the worksheet.键 '#' 被截获,但如果您在单元格中处于输入模式,则不会截获,前提是焦点位于工作表中的某个位置。

I think you'll have to do this outside of Excel using a keyboard remapping tool for Windows.我认为您必须使用 Windows 的键盘重新映射工具在 Excel 之外执行此操作。 I quickly found https://www.howtogeek.com/710290/how-to-remap-any-key-or-shortcut-on-windows-10/ by googling but know nothing about it or if it is legit or not.我通过谷歌搜索很快找到了 https://www.howtogeek.com/710290/how-to-remap-any-key-or-shortcut-on-windows-10/但对此一无所知,或者它是否合法。

Have you considered just using Shift + Enter to insert a carriage return instead?您是否考虑过只使用Shift + Enter来插入回车?

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

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