excel的VBA输入框输入验证问题

时间:2015-05-17 01:43:27

标签: vba validation excel-vba excel

Excel要求:用户通过输入对话框输入数字。在当前选择的指定(硬编码)列中包含该数字的行将被删除。

关于输入验证,我没有提出或找到涵盖所有这些问题的解决方案:

  1. 不接受空输入。 (有些变化我尝试将其视为零。)首先会显示错误,但如果没有更好的方法,可以像取消按钮那样对待。
  2. 取消按钮是取消对话框而不显示其他消息。 (在下面的代码中,它会导致"输入无效。"
  3. 必须允许零值输入。 (在我的一些尝试中不被视为取消)
  4. 没有零匹配时的零值必须显示"无需删除任何内容。"
  5. 如果单元格为空,则不删除行。即。不要将零与空单元格匹配。
  6. 我尝试过各种代码,但每个代码中至少有一个问题。以下代码是从我原来的帖子修改的。到目前为止,这是我最好的解决方案。唯一的问题是取消导致"没有输入值。" 感谢。

    Sub DeleteRows()
    On Error GoTo InvalidInput
    Dim InputRng As Range
    Dim DeleteRng As Range
    Dim DeleteNum As Double
    Dim DeleteV As Variant
    Dim CountDelete As Long
    Dim CountAreas As Long
    Dim Str As String
    Set InputRng = Application.Selection.Rows
    
    DeleteV = Application.InputBox("Match Value", "Delete Rows")
    'TODO Ultimate solution: Check for cancel so as not to result in "No value entered".
    'NOTE: If DeleteV = Empty is true upon cancel
    'NOTE this unwantingly matches zero: If DeleteV = False Then Exit Sub
    'Check something was entered
    If DeleteV = Empty Then
        MsgBox ("No value entered.")
        Exit Sub
    End If
    DeleteNum = CDbl(DeleteV)
    
    'Accumulate what is to be deleted
    For Each r In InputRng.Rows
        'Only if cell is a number
        If Not IsEmpty(r.Cells(3)) Then
            If IsNumeric(r.Cells(3).Value) Then
                If r.Cells(3).Value = DeleteNum Then
                    If DeleteRng Is Nothing Then
                        Set DeleteRng = r
                    Else
                        Set DeleteRng = Application.Union(DeleteRng, r)
                    End If
                End If
            End If
        End If
    Next
    If DeleteRng Is Nothing Then
        MsgBox ("Nothing to delete.")
        Exit Sub
    End If
    
    CountDelete = 0
    'The range may contain separated areas,
    'so loop through each area to accumulate the total count
    For Each a In DeleteRng.Areas
        CountDelete = CountDelete + a.Rows.Count
    Next a
    If CountDelete > 1 Then
        Str = " rows."
    Else
        Str = " row."
    End If
    'Confirm delete
    If MsgBox("Confirm delete " & CountDelete & Str, vbYesNo) = vbYes Then
        DeleteRng.EntireRow.Delete
    End If
    Exit Sub
    InvalidInput:
        MsgBox ("Invalid input.")
    End Sub
    

1 个答案:

答案 0 :(得分:0)

尝试使用类型1

<强>语法

expression.InputBox(提示,标题,默认,左,顶部,帮助文件,帮助文本ID,类型)

<强>类型

下表列出了可以在Type参数中传递的值。可以是一个值或一个值的总和。例如,对于可以接受文本和数字的输入框,请将“类型”设置为1 + 2。

Value Meaning 
0     A formula 
1     A number 
2     Text (a string) 
4     A logical value (True or False) 
8     A cell reference, as a Range object 
16    An error value, such as #N/A 
64    An array of values 

DeleteV = Application.InputBox("Match Value", "Delete Rows", Type:=1)

Sub Sample()
    '~~> Only valid numbers are allowed including 0
    '~~> Empty input will not to be accepted.
    DeleteV = Application.InputBox("Match Value", "Delete Rows", Type:=1)

    '~~> If user presses Cancel
    If DeleteV = "False" Then Exit Sub

    '~~> If user enters a number else it won't execute this
    If DeleteV >= 0 Then
        '
        '~~> Rest of your code
        '
    End If
End Sub

您需要处理的积分

  1. 不接受空输入。
  2. 取消按钮是取消对话框而不再发送消息。
  3. 必须允许零值输入。 (在我的一些尝试中不被视为取消)
  4. 没有零匹配时的零值必须显示“无需删除”。 可在If-EndIf
  5. 内处理
  6. 如果单元格为空,则不删除行。即。不要将零与空单元格匹配。 可在If-EndIf
  7. 内处理