如何在Visual Basics Excel中的Key上使用Application [英] How to use Application on Key in Visual basics Excel
问题描述
我想在vba excel宏中使用键输入来执行操作. 我已经在互联网上搜索了一段时间,发现按键上的应用程序"可能有用.但是我还不知道如何使用它.
I'd like to use key inputs in my vba excel macro to preform actions. I've been searching the internet for a while and found "Application on Key" might be usefull. But I don't understand yet how to use it.
在这种情况下,我的目标是通过使用箭头键移动活动单元格. (Excel已经做到了).但是,请执行一些其他操作:首先,需要将其减慢一定的毫秒数,并且activecell需要更改颜色"ActiveCell.Color.Index = RGB(255,0,0)".
In this case my goal is tho use the arrow keys to move the active cell. (which excel already does). But do some additional actions: Firts it needs to be slowed down by a veriable amount of ms, and the activecell needs to change color "ActiveCell.Color.Index = RGB ( 255,0,0)" for example.
使用"A/S/W/D"键进行导航会更容易,两个我会很高兴.
It it would be easyer to use "A/S/W/D" key's to navigate I would be happy two.
目标:某种蛇游戏
问题:如何使用按键输入?
推荐答案
您需要在宏中使用Application.OnKey "[Button]", "[sub to start]"
-命令通过按下一个键来启动宏,但请记住:
You need in a macro the Application.OnKey "[Button]", "[sub to start]"
-command to start a macro by pushing a key but keep in mind:
该宏必须位于Module
中,或者您需要添加以下路径:
the macro needs to be in a Module
or you need to add the path like:
Sub Test()
Debug.Print "OK"
End Sub
'if test is in Module
Application.OnKey "{RIGHT}", "Test"`
'if test is in sheet/userform
Application.OnKey "{RIGHT}", "Sheet1.Test"`
Application.OnKey "{RIGHT}", "UserForm1.Test"`
每按一次右箭头按钮",就会执行测试".要停用它,只需在没有目标的情况下运行Application.OnKey "{RIGHT}"
.
"Test" will be executed everytime the "right arrow button" is pressed. To deactivate it just run Application.OnKey "{RIGHT}"
without a target.
Application.OnKey
不会设置回零.因此,如果未在关闭/切换/无论工作簿如何设置自动停用功能时,都可能会收到VBA错误.
Application.OnKey
will not set back to nothing if the workbook is closed or something like that. So you may get VBA-Errors when not set a automatic for deactivation on closing/switching/whatever the workbook.
这篇关于如何在Visual Basics Excel中的Key上使用Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!