【问题标题】:EXCEL VBA Filter user inputEXCEL VBA 过滤用户输入
【发布时间】:2013-04-16 05:31:37
【问题描述】:

我的输入框允许用户在单元格中输入一个值。因此,我希望过滤一些输入,如=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

【问题讨论】:

  • 您使用什么类型的输入框?向我们展示您的代码的简单部分以及稍后如何将数据输入到单元格
  • 更新了我的问题中的代码块

标签: excel vba


【解决方案1】:

您可以使用不同的尝试来过滤某些或所有必需的值。请记住,您 y variable 是字符串类型。因此,这里有一些 cmets 的一些想法:

'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

您可以使用andor 运算符将它们全部组合成一个if...then 语句。

【讨论】:

  • 感谢您的回答!我猜过滤器'='足以防止用户将公式输入到单元格中。但我想知道为什么 ' 不会显示它是否是第一个字符。
猜你喜欢
  • 1970-01-01
  • 2018-10-14
  • 2011-08-23
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
相关资源
最近更新 更多