【问题标题】:Merging Private Sub functions in Excel VBA在 Excel VBA 中合并私有子函数
【发布时间】:2012-10-10 23:15:05
【问题描述】:

我编写了三个不同的 Private Sub 函数,它们都做同样的事情 Private Sub txt_noRooms_KeyPressPrivate Sub txt_length_KeyPress 确保用户只能在引用的文本字段中输入值 1 到 9,而 Private Sub txt_studentNo_KeyPress 允许用户在引用的文本字段中输入值 0 到 9。

有什么方法可以合并这三个函数,让它们仍然支持引用的单元格并保持相同的条件?我想这样做是为了使代码更高效。

Private Sub txt_noRooms_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_noRooms
Select Case KeyAscii
    Case Asc("1") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_noRooms.Text, "-") > 0 Or Me.txt_noRooms.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_noRooms.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub


Private Sub txt_length_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)   
' Restrict entry to txt_length
Select Case KeyAscii
    Case Asc("1") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_length.Text, "-") > 0 Or Me.txt_length.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_length.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub


Private Sub txt_studentNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_studentNo
Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_studentNo.Text, "-") > 0 Or Me.txt_studentNo.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_studentNo.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以设置一个私有函数并从每个按键调用它:

      Private Sub txt_noRooms_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_noRooms
    If bclearfield(CLng(KeyAscii), Me.txt_noRooms.Text, Me.txt_noRooms.selStart,"1")  Then KeyAscii = 0
    End Sub
    
    
    Private Sub txt_length_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_length
    If bclearfield(CLng(KeyAscii), Me.txt_length.Text, Me.txt_length.selStart,"1") Then KeyAscii = 0
    End Sub
    
    Private Sub txt_studentNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_studentNo
    If bclearfield(CLng(KeyAscii), Me.txt_studentNo.Text, Me.txt_studentNo.selStart,"0") Then KeyAscii = 0
    End Sub
    
    Private Function bClearField(KeyAscii As Long, sText As String, lSelStart As Long,sLowLimit as string) As boolean
    
    bClearField=false
    Select Case KeyAscii
        Case Asc(sLowLimit ) To Asc("9")
        Case Asc("-")
            If InStr(1, sText, "-") > 0 Or lSelStart > 0 Then
                bClearField=true
            End If
        Case Asc(".")
            If InStr(1, sText, ".") > 0 Then
                bClearField=true
            End If
        Case Else
            bClearField=true
    End Select
    
    End Function
    

    【讨论】:

    • 我现在似乎无法在字段中输入任何内容
    • 您是否尝试过在其中一个按键子按钮中设置断点 (F9) 并进入代码 (F8) 以查看问题所在?
    • 我已经更改了函数返回的 var 类型。不知道会不会有帮助。
    • 每当我尝试在字段中输入内容时,都会收到运行时错误 91
    • Case Else GetKeyAscii = 0 End Select
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2020-08-27
    • 1970-01-01
    • 2014-11-15
    • 2015-12-10
    相关资源
    最近更新 更多