【问题标题】:Clear contents leave formula if cell change如果单元格更改,清除内容离开公式
【发布时间】:2020-04-20 15:29:34
【问题描述】:

更改单元格 B12 的值(下拉菜单)时,单元格 B13 应提及“请选择...”,并且应清除范围 A16:N20 中的所有单元格,但不是公式(每个单元格范围内包含链接到单元格 B12、B13 和其他...的索引/匹配公式。这是当前不起作用的代码...

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$B$12" Then

        With Application
            .DisplayStatusBar = False
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With

    Range("B13").Value = "Please Select..."
    Range("A16:N20").SpecialCells(xlCellTypeConstants, 23).ClearContents

        With Application
            .Calculation = xlCalculationAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
            .DisplayStatusBar = True
        End With
    End If
End Sub

此外,Excel 必须更新所有这些索引/匹配公式非常慢 - vba 中有没有办法让它更快?

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    Dim r as Range
    
    for each r in Range("A16:N20").Cells
      if WorksheetFunction.isFormula(r) = "" then
        r.clearcontents
      end if
    next r
    

    如您所见,您可以使用IsFormula() 工作表函数来解决这个问题。

    【讨论】:

    • 没有WorksheetFunction.FormulaText 方法。 WorksheetFunction.IsFormula 可能是你的意思?
    • 糟糕,抱歉。是的。
    • 谢谢,当我在单独的子系统中使用它时,它确实可以正常工作。但是,我想将它集成到现有的子中,以便在单元格 B12 中的下拉列表更改时它可以工作。这可能吗?
    • @Debby:我根据这篇文章“stackoverflow.com/questions/3875415/…”调整了我的答案。你能检查它是否有效吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2023-04-10
    相关资源
    最近更新 更多