【问题标题】:How to reject symbols in application.inputbox如何拒绝 application.inputbox 中的符号
【发布时间】:2021-08-10 20:38:12
【问题描述】:

我在下面有一些代码可以创建一个输入框,我不想让人们在其中输入符号。当有人输入一个拒绝输入的符号时,我可以做些什么来弹出一个 msgBox,或者完全禁止他们?代码粘贴在下面。感谢您的任何帮助:)

If AnswerYes = vbYes Then

Dim Discount1 As Variant
Discount1 = Application.InputBox("DO NOT ENTER % SIGN", "Product Discount Percentage", "##", Type:=1)

Dim Discount2 As Variant
Discount2 = Application.InputBox("DO NOT ENTER % SIGN", "SNS Discount net Percentage", "##", Type:=1)

【问题讨论】:

  • 在这种情况下最好的办法是创建一个用户表单(如果你愿意,它可能看起来与输入框完全一样),并捕获Textbox_Change 事件。在该事件代码中,检查键并只允许显示可接受的击键并忽略其他所有内容。查看this video 以获得更多指导。

标签: excel vba inputbox


【解决方案1】:

这应该可行。 False 响应是针对 cancel。它会拒绝任何不是数字的文本。如果您想要更复杂的东西,请考虑使用 ascii 代码。

Sub makeInput()
makeEntry:
    theResponse = Application.InputBox("DO NOT ENTER % SIGN", "Product Discount Percentage", "##", Type:=1)
    
    If theResponse = False Then Exit Sub

    Dim i As Long
    For i = 1 To Len(theResponse)
        If Not (IsNumeric(Mid(theResponse, i, 1))) Then
            MsgBox "Invalid Entry"
            GoTo makeEntry
        End If
    Next i
End Sub

【讨论】:

    【解决方案2】:

    像这样创建一个用户表单

    然后将此代码粘贴到用户窗体代码区域。 TextBox1_KeyPress 将确保用户不会输入除数字和小数之外的任何内容。

    Option Explicit
    
    Private Sub UserForm_Initialize()
        MyValue = 0: Cancelled = False
    End Sub
    
    '~~> OK Button
    Private Sub CommandButton1_Click()
        If Len(Trim(TextBox1.Text)) = 0 Then
            MsgBox Label1.Caption
            Exit Sub
        End If
        
        MyValue = Val(TextBox1.Text)
        Unload Me
    End Sub
    
    '~~> CANCEL Button
    Private Sub CommandButton2_Click()
        Unload Me
        Cancelled = True
    End Sub
    
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Select Case KeyAscii
            Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyDelete, vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
                If KeyAscii = 46 Then If InStr(1, TextBox1.Text, ".") Then KeyAscii = 0
            Case Else
                KeyAscii = 0
                Beep
        End Select
    End Sub
    

    现在你可以像这样使用它了。将此代码粘贴到模块中

    Option Explicit
    
    Public MyValue As Double
    Public Cancelled As Boolean
    
    Sub Sample()
        Dim frm As New UserForm1
        Dim Discount1 As Double
        Dim Discount2 As Double
    
        With frm
            .Caption = "WhatEver Title"
            .Label1.Caption = "Enter Product Discount Percentage"
            .Show
        End With
        
        If Cancelled = False Then
            Discount1 = MyValue
            MsgBox Discount1
        End If
        
        Set frm = New UserForm1
        
        With frm
            .Caption = "WhatEver Title"
            .Label1.Caption = "Enter SNS Discount net Percentage"
            .Show
        End With
        
        If Cancelled = False Then
            Discount2 = MyValue
            MsgBox Discount2
        End If
    End Sub
    


    编辑

    如果需要,可以为模块代码创建一个通用函数。

    Option Explicit
    
    Public MyValue As Double
    Public Cancelled As Boolean
    
    Sub Sample()
        Dim Discount1 As Double
        Dim Discount2 As Double
    
        Discount1 = ShowInputBox("WhatEver Title", "Enter Product Discount Percentage")
        
        If Cancelled = False Then MsgBox Discount1
        
        Discount2 = ShowInputBox("WhatEver Title", "Enter SNS Discount net Percentage")
        
        If Cancelled = False Then MsgBox Discount2
    End Sub
    
    Private Function ShowInputBox(Title As String, Msg As String) As Double
        Dim frm As New UserForm1
    
        With frm
            .Caption = Title
            .Label1.Caption = Msg
            .Show
        End With
        
        If Cancelled = False Then ShowInputBox = MyValue
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2014-12-17
      相关资源
      最近更新 更多