在用户窗体的文本框中的箭头键,VBA

我正在用几个文本框来处理一个用户窗体,这些文本框就像一个Excel图表一样排列。 我已经设置了TabIndexes,并且使用TAB / shift + TAB完美地工作。 但是,按箭头键的反应并不是我所期望的。 我命名这些文本框:

boxA1 boxB1 boxC1 boxD1 boxA2 boxB2 boxC2 boxD2 ... boxA3 boxB3 boxC3 boxD3 : : 

假设焦点在boxB1。 当按下箭头键时,我希望焦点转到boxB2,但是会转到boxA3或其他东西。

我试过这个代码:

 Private Sub boxB1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) Select Case KeyCode Case vbKeyDown boxB2.SetFocus Case vbKeyUp boxA10.SetFocus End Select End Sub 

它工作正常,但是,如果在我的表单中只有几个文本框,这是一个伟大和清晰的解决scheme。 但是我的表单中有大约70个文本框,这会使我的代码变得庞大而且真的重复。 有任何参数可以调整,以使其正确吗? 或者有什么function,我可以使用如下? 只是sudo,你知道的。

 Private Sub name_i_KeyDown(ByVal KeyCode as MSForms.ReturnInteger, ByVal Shift As Integer) Select Case KeyCode Case vbKeyDown Controls("name_" & i+1).SetFocus Case vbKeyUp Controls("name_" & i-1).SetFocus End Select End Sub 

谢谢!

你走在正确的道路上 在表单模块中添加以下两个UDF:

 Sub SetTextBoxFocus(ByVal KeyCode As MSForms.ReturnInteger) Dim sPrevControl As String Dim sNextControl As String Dim sBoxToSet As String Dim oC As Control ' Cater for TAB key press. Setting it so that it behaves like vbKeyDown. Remove this If condition if not required If Asc(KeyCode) = 57 Then KeyCode = vbKeyDown End If ' We only want to check if pressed key was either vbKeyDown or vbKeyUp and the key was pressed on a TextBox If TypeName(Me.ActiveControl) = "TextBox" And (KeyCode = vbKeyDown Or KeyCode = vbKeyUp) Then With Me.ActiveControl ' Lets set both previous and next text box names If InStr(1, .Name, "boxD") > 0 Then sPrevControl = "boxC" & GetNumFromString(.Name) sNextControl = "boxA" & GetNumFromString(.Name) + 1 ElseIf InStr(1, .Name, "boxA") > 0 Then sPrevControl = "boxD" & GetNumFromString(.Name) - 1 sNextControl = "boxB" & GetNumFromString(.Name) Else sPrevControl = "box" & Chr(Asc(Mid(.Name, 4, 1)) - 1) & GetNumFromString(.Name) sNextControl = "box" & Chr(Asc(Mid(.Name, 4, 1)) + 1) & GetNumFromString(.Name) End If ' Bases on which key was pressed, lets set the name of the text box to move to Select Case KeyCode Case vbKeyDown: sBoxToSet = sNextControl Case Else: sBoxToSet = sPrevControl End Select ' Loop through all controls in the form and set the focus to the control if found For Each oC In Me.Controls If oC.Name = sBoxToSet Then oC.SetFocus Exit For End If Next End With End If End Sub Function GetNumFromString(ByVal txt As String) As String Dim oRe As New RegExp With oRe .pattern = "\d" If .Test(txt) Then GetNumFromString = .Execute(txt)(0) End With End Function 

现在,在每个TextBox的KeyDown中,进行以下调用: SetTextBoxFocus KeyCode 。 这应该做的伎俩

注:我有boxD作为最后一个文本框。 您应该将其更改为您在同一行中的最后一个文本框