在Excel中创建宏以突出显示大于控制单元格的单元格

时间:2016-10-04 07:11:02

标签: excel vba excel-vba

如果它大于控件值,我想制作一个突出显示行中特定cel的宏。我的Excel表格如下:

So for product A E2 needs to get a red color

因此对于产品A E2需要获得红色。因为它比单元格K2中的值大。对于产品B,细胞E和H需要得到红色,因为它们大于K3。产品C不会有任何颜色。这张表只是一个例子。在最终的表格中,将有700多种产品,因此行数需要调整。

我想我需要某种循环,但不知道如何设置它。

希望有人能帮助我。

提前感谢您的时间和精力!

J Rommers

2 个答案:

答案 0 :(得分:1)

这是一项相当容易的任务,你可以通过搜索和尝试来轻松完成它。

但这是代码:

Dim sheetName As String
Dim startRow As Integer, startCol As Integer
Dim endRow As Integer, endCol As Integer
Dim row As Integer, col As Integer

sheetName = "Sheet1" 'Your sheetname

With Sheets(sheetName)

    startRow = 2 'start row for the loop
    startCol = 2 'start column for the loop

    endRow = .UsedRange.SpecialCells(xlCellTypeLastCell).row 'Last Used Row
    endCol = .UsedRange.SpecialCells(xlCellTypeLastCell).Column 'Last Used Column

    For row = startRow To endRow Step 1 'Loop through rows

        For col = startCol To endCol - 1 Step 1 'Loop through columns | Leave out the Peak Column

            If .Cells(row, col).Value > .Cells(row, endCol).Value Then 'If value of cell is bigger than peak column

                .Cells(row, col).Interior.Color = vbRed 'mark cell in red
            End If
        Next col
    Next row
End With

答案 1 :(得分:1)

你也可以尝试使用"条件格式化"小案例场景的规则。 选择要应用规则的单元格,单击"新规则" select"仅格式化包含"然后"单元格值大于K5"例如,选择所需的单元格格式。 这也可以复制到其他单元格,并根据具体情况进行更改。

相关问题