EXCEL VBA过滤用户输入

时间:2013-04-23 05:53:22

标签: excel vba

MY inputbox允许用户将值输入到单元格中。因此,我希望过滤一些输入,如=SUM(A:A)',这些输入将不显示任何内容,以及其他可能影响实际值的公式或可能的输入。提前谢谢。

Private Sub testInputBox_Click()
    Dim x As String
    Dim y As String
    Dim yDefault As String
    Dim found As Range

    x = InputBox("Enter Parts No.", "Edit Description")

    If (x <> "") Then
        If (WorksheetFunction.CountIf(Worksheets("Sheet1").Range("E2:E27"), x) > 0) Then
            Set found = Worksheets("Sheet1").Range("E2:E27").Find(x, LookIn:=xlValues)
            yDefault = found.Offset(0, 1).Text
            y = InputBox("Amend Description", "Edit Description", yDefault)
        Else
            MsgBox ("Not found!")
        End If

        If (y <> "") Then 'Filter should be done here
            If MsgBox("Proceed to edit?", vbYesNo, "Confirmation") = vbNo Then
            Else
                found.Offset(0, 1).Value = CStr(y)
            End If

        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用不同的尝试来过滤部分或全部所需的值。请记住,y variable是字符串类型。因此,以下是一些有一些意见的想法:

'tests for starting characters
If y <> "'" And y <> "=" Then
    'test for formulas
    If UCase(Left(y, 4)) <> "=SUM" Then
        'test for any string within other string
        If InStr(1, y, "sum", vbTextCompare) = 0 Then
            '...here your code
        End If
    End If
End If

您可以使用if...thenand运算符将它们全部合并到一个or语句中。

相关问题