Excel 2015, freezing a cell's figure when conditions are met

时间:2015-11-12 12:02:50

标签: excel

I'm using Excel 2015. When the figure in a cell reaches or goes over a limit, I would want that cell to lock/freese, so that when I come back to it I can still see that the limit was reached.

The problem is that it's a live chart and the figure can go back down below the limit, knocking of the indicator that tells me the limit was reached. I can set a warning/indicator for when a limit is reached but can't work out how to freeze the limit being reached. This freezing can be in the cell beside if needs be.

1 个答案:

答案 0 :(得分:0)

使用标准函数没有简单的方法,但您可以使用User Defined Function

在“开发人员”选项卡上,选择“查看代码”,然后查看工作簿的代码 - 这将打开Visual Basic编辑器。

选择Insert - > Module,您应该会在树中看到“模块”分支,展开它并双击Module1(您刚插入的)。

添加以下代码

Public Curmax(10) As Double
Public Reset As Boolean
Function RunMax(Cell As Range, maxref As Integer) As Double
    Application.Volatile (True) 'Ensures UDF is run when the sheet recalculates
    If Cell.Value > Curmax(maxref) Then
        Curmax(maxref) = Cell.Value
    End If
    RunMax = Curmax(maxref)
End Function
Sub ClearRunMax()
    With ActiveCell
        maxref = Mid(.Formula, InStr(.Formula, ",") + 1, 1)
        Curmax(maxref) = 0
        ActiveSheet.Calculate
    End With
End Sub

现在,如果您在单元格中输入=RunMax(A1,1),它将保持单元格A1中最大参考号的运行最大值。 1,添加另一个,然后只需增加引用 - =RunMax(C3,2), =RunMax(F56,3),最多为10(或放入Public Curmax(10) As Double中的任何内容)

如果您在页面上放置一个按钮,为其指定ClearRunMax宏,它会将最大值重置为您正在跟踪的单元格的当前值(假设它大于0)如果您选择了包含RunMax语句的调用,然后单击按钮。

这可以改进,我将是第一个承认。例如,没有错误检查。因此,当您按下按钮时选择了错误的单元格,您将获得Type Mismatch。此外,如果您的值为负,则重置将不起作用。