Userform事件和调用私有子

时间:2014-04-03 14:29:41

标签: excel excel-vba vba


我有userbox和textboxes。文本框限制某些字符的输入,或者更确切地说允许输入数字逗号和点。代码在文本框的key_press事件范围内。只要下面的代码在key_press事件中,一切正常。当我使用来自不同子代码的相同代码输入call private sub时,它不起作用。

为什么它不起作用?

Key_press事件中的代码:

Private Sub netto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii

    Case Asc("0") To Asc("9")
    Case Asc("-")
    Case Else
    KeyAscii = 0

End Select

End Sub

Key_press事件中使用call sub的代码:

Private Sub netto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Call klawisze

End Sub

Private Sub klawisze()

Select Case KeyAscii

    Case Asc("0") To Asc("9")
    Case Asc("-")
    Case Else
    KeyAscii = 0

End Select

End Sub

1 个答案:

答案 0 :(得分:1)

你在哪里接近正确的解决方案。您只需要在KeyAscii子区域中传递参数klawisze(它接受相应的参数:ByVal KeyAscii As MSForms.ReturnInteger):

Private Sub netto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Call klawisze(KeyAscii)
End Sub

Private Sub klawisze(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Asc("-")
        Case Else
            KeyAscii = 0
    End Select
End Sub