首页 > 解决方案 > 如果值小于另一个单元格值,则清除 Excel 单元格

问题描述

我希望有人可以提供帮助,当值小于另一个单元格中的值时,我需要清除单元格。我确实使用了条件格式,但这会使工作表中的计算更加混乱。

当我将固定整数输入模块时,我使用了指南并且能够删除单元格,但不确定我如何调整它以引用单元格而不是固定数字。

谢谢你。

埃德

标签: vbaexcel

解决方案


我相信这就是您在下面寻找的内容,这将从单元格 B1 中获取一个值并与 A 列中的值进行比较,如果这些值小于 B1 中的值,它将清除该单元格的内容:

Sub ClearLowerThan()
    Dim c As Range, Rng As Range
    Dim LastRow As Long        
    Dim ws As Worksheet: Set ws = Sheets("Sheet1")
    'declare you worksheet, amend as required
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    'get the last row with data on Column A

    CompareVal = ws.Range("B1").Value
    'get the value to compare against, in this case from cell B1

    Set Rng = ws.Range("A1:A" & LastRow)
    'set the range to compare against the value from B1

    For Each c In Rng 'for each cell in the given range
        If c.Value < CompareVal Then c.ClearContents
        'if value of cell is less than the value to compare against, clear the cell contents.
    Next
End Sub

推荐阅读