根据另一个单元格的数值更改x个单元格的颜色 [英] Change the color of x number of cells based on the numeric value from another cell

查看:76
本文介绍了根据另一个单元格的数值更改x个单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有范围,我想根据我在单元格Range("C5")中输入的值来突出显示我的单元格范围.如果我在单元格"C5"中输入5,则需要将我范围内的5个单元格更改为黄色.

I have range and i want to highlight my range of cells based value i enter in cell Range("C5"). If i enter 5 in cell "C5" then 5 cells in my range needs to be changed to yellow color.

Dim MY_RANGE As Range
Dim VALUE1 As Integer
Dim CEL As String

Set MY_RANGE = Range("H8,J8,L8,N8,H10,H10,J10,L10,N10,H12,J12,L12,N12,H14,J14,L14,N14,H16,J16,L16,N16,N16")
VALUE1 = Range("C2")

For Each CEL In MY_RANGE.Cells
    If CEL.Value = VALUE1 Then
        With CEL
                 .Italic = False
                .Bold = True
                .Color = 255
                .TintAndShade = 0
        End With
    End If
Next CEL

有人可以帮忙吗?.

推荐答案

您可以使用 Range()对象的 Areas()属性:

you can use Areas() property of Range() object:

Public Sub ColorCells()
    Dim iCell As Long

    With Range("H8,J8,L8,N8,H10,J10,L10,N10,H12,J12,L12,N12,H14,J14,L14,N14") ' reference your range
        For iCell = 1 To Range("C2").Value ' loop through needed areas
            .Areas(iCell).Interior.Color = vbYellow
        Next
    End With
End Sub

注意:在您的 MY_RANGE 范围内,出现了两次"H10"事件

Note: in your MY_RANGE range there were two "H10" occurrences

这篇关于根据另一个单元格的数值更改x个单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆