Excel VBA根据值更改单元格的颜色 [英] Excel VBA Change Color Of Cell Based On Value

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

问题描述

我需要一个函数的帮助,该函数可以根据单元格的值是否等于另一个单元格的值来更改单元格的颜色,目前该单元格具有= COUNT(##:##),而我如果该值等于另一个单元格中的值,则需要将颜色从红色更改为绿色.

I need some help with a function that can change the color of the cell based on if the value of the cell is equal to the value of another, currently the cell holds an =COUNT(##:##) and I need to to Change the color from red to green if that value is equal to the value in another cell.

由于对VBA和excel的了解有限,我想到了这一点

With my limited knowledge of VBA and excel I came up with this

Function ChangeColor(CellColor As Range)
Application.Volatile True
If CellColor = cell.Value Then ChangeColor = cell.Interior.ColorIndex = 14
End Function

如果可能的话,我宁愿不使用条件格式,但是我愿意在需要时使用它.感谢您的所有帮助,并帮助我解决了先前的问题,这个社区很棒.

I would rather not use Conditional Formatting if at all possible, but I am open to it as a last resort if needed. Thanks for all your help and for helping me with previous questions this community is great.

推荐答案

我做了下面的代码,以突出显示同一列中的单元格.条件格式不允许我级联等于上面的单元格"的公式,也许我只是做得不好.无论如何,这是可行的,并且很容易更改和添加.可以轻松地更改此示例以适合您的问题.

I did the code below to highlight cells in a column that were the same. The conditional formatting didn't let me cascade the formula of "equal to the cell above", maybe I just didn't do that right. Anyway this worked and is easy to change around and add to it. This example could easily be changed to suit your problem.

Sub checkduplicates()
Dim Loop1 As Integer
Dim Loop1StartRow As Integer
Dim Loop1EndRow As Integer
Dim Loop1Count As Integer
Dim Current As Integer
Dim NextOne As Integer

Loop1StartRow = 4 '4 HYIDAS
Loop1EndRow = 330 '330 HYIDAS
Loop1Count = 0

For Loop1 = Loop1StartRow To Loop1EndRow
    Worksheets("HYIDAS").Activate
    Loop1Count = Loop1Count + 1
    Current = Range("H" & Loop1)
    NextOne = Range("H" & Loop1 + 1)
    If Current = NextOne Then
        Range("H" & Loop1).Interior.Color = 220
        Range("H" & Loop1 + 1).Interior.Color = 220
    End If
Next Loop1
End Sub

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

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