我继承了一个相当深入和复杂的excel工作簿,我的任务是添加到上面。
工作簿是为用户在第一页的文本框中输入信息而设计的,按enter会触发一个keyup事件。
我很好奇是否有办法从另一张纸上的按钮事件中调用这个例程??
Public Sub txtbx_length_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case Asc(vbCr), Asc(vbLf), Asc(vbCrLf):
Application.Calculation = xlCalculateManual
process_length_val Me.txtbx_length.Value
Me.txtbx_length.Value = ""
Me.txtbx_length.Activate
Application.Calculation = xlCalculationAutomatic
Case Asc("E"): ' , Asc("e"):
' In tests, regardless if I had the shift key pressed or caps lock,
' or nothing, I always seen "E" come across -- never "e".
Application.Calculation = xlCalculateManual
process_length_val Me.txtbx_length.Value
Me.txtbx_length.Value = ""
Me.txtbx_length.Activate
Application.Calculation = xlCalculationAutomatic
End Select我想避免发送密钥,因为它根本不可靠,取决于什么有焦点。尤其是使用enter键。
我试过:
txtbx_length_KeyUp和
call txtbx_length_KeyUp但两者都给出了一个错误:“未定义的子或函数”。
我很好奇是否有办法从另一张纸上的按钮事件中调用这个例程??
发布于 2022-10-04 11:47:10
为了查看它是否/如何工作,请尝试下一种方法:
KeyCode看上去并不是Integer。它是一个对象/类!具有通过键盘传递的单个(value)属性。因此,如果您能够使用/创建必要的参数,则可以从另一个Sub调用一个事件。请将下一个声明复制到标准模块(在声明区域)之上: Public KeyCodeObj As MSForms.ReturnIntegerPublic Sub txtbx_length_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCodeObj Is Nothing Then Set KeyCodeObj = KeyCode 'the necessary line, to set the necessary objec (to be used when calling the event)
Select Case KeyCode
Case Asc(vbCr), Asc(vbLf), Asc(vbCrLf):
Application.Calculation = xlCalculateManual
process_length_val Me.txtbx_length.Value
Me.txtbx_length.Value = ""
Me.txtbx_length.Activate
Application.Calculation = xlCalculationAutomatic
Case Asc("E"): ' , Asc("e"):
' In tests, regardless if I had the shift key pressed or caps lock,
' or nothing, I always seen "E" come across -- never "e".
Application.Calculation = xlCalculateManual
process_length_val Me.txtbx_length.Value
Me.txtbx_length.Value = ""
Me.txtbx_length.Activate
Application.Calculation = xlCalculationAutomatic
End Select
End SubPrivate Sub callKeyUpEvent()
Const sheetName As String = "Report" 'use here your sheet name where the text box in discussion exists
Const directCallMask As Integer = 8 'direct-call functionality
KeyCodeObj.Value = vbKeyReturn 'Enter. Or any other key code to be sent...
Call Worksheets(sheetName).txtbx_length_KeyUp(KeyCodeObj, directCallMask)
End Sub现在,txtbx_length Enter,文本框中的写任何东西,或者只按来设置必要的KeyCodeObj对象.
callKeyUpEvent过程来调用事件.注意:由于KeyCodeObj变量是易失性的(仅存在于Excel内存中),因此在出现错误后出现代码中断时,它可能会消失。因此,一些检查和重新设置对象变量的方法可能是必要的。即使不是那么简单,也需要在相应的文本框中按下真正的键。也许,这样的模拟是可能的,但我要说的是,这并不能成为问题的目标:)
上面的解决方案只意味着做你想做的事情是可能的(以这种方式),但它并不是一个**真正可靠的解决方案“!特别是,因为设置必要的对象很困难,而不是从事件中捕获它(至少,我不知道它是如何可能的)。
一个可靠的解决方案--以一种可靠的方式执行您需要的--,将是下一个解决方案:
创建一个被KeyCode转换为Integer的调用的sub,事件将调用它。请将下一段代码复制到标准模块中
Sub DoTheJob(keyCode As Integer)
Select Case keyCode
Case vbKeyReturn
MsgBox "Enter key has been pressed..."
'place here your code to be triggered by that specific key (`Enter`)...
Case vbKeyE
MsgBox "E key has been pressed..."
'place here your code to be triggered by that specific key (`E`)...
End Select
End SubB.现有的活动代码将修改如下:
Public Sub txtbx_length_KeyUp(ByVal keyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCodeObj Is Nothing Then Set KeyCodeObj = keyCode 'only to offer the possibility to direcly call THE EVENT
DoTheJob CInt(keyCode) 'cast the object to a simple Integer...
End SubDoTheJob子,但是也可以直接调用(或任何其他事件、过程等)。as:Sub testDoTheJob()
DoTheJob vbKeyReturn
DoTheJob vbKeyE
End Subhttps://stackoverflow.com/questions/73938771
复制相似问题