【excel中】如何在textbox1中按键盘上"ENTER"键就运行CommandButton1中的代码?
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 展开
使用onkey
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "{ENTER}" '大键盘回车键
Application.OnKey "~" ’数字盘上回车键
End Sub
Private Sub Workbook_Open()
Application.OnKey "{ENTER}", "回车程序代码"
Application.OnKey "~", "回车程序代码"
End Sub
2. 使用Worksheet_Change
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$4" And Cells(3, 1) <> "" And Cells(4, 1) <> "" Then
'要执行代码
End If
End Sub
3.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Application.MoveAfterReturn = xlDown
ThenIf Target.Row = 3 And Target.Column = 1
ThenCells(Target.Row - 1, 2) = "A"
End If
End If
End Sub
http://hi.baidu.com/mizuda/item/786d7a77b1c96039714423b7
为什么不把CommandButton1设好顺序,当text发生lostfocus后设为默认呢?
keyCode =13,这代码我是知道的,更想知道如何关联CommandButton1的click方法,写在CommandButton1的click方法代码段就不要重复cope了,而不是text发生lostfocus这个事件?
能帮忙整下VBA代码段吗?
2014-01-26
TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+Button1.UniqueID+"').click();return false;}} else {return true}; ");