Excel宏根据多个单元格条件更改单元格格式 [英] Excel Macro to Change cell formatting based on multiple cell conditions

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

问题描述

我需要一个简短的excel宏来根据单行中两个单元格的值更改列中单元格的格式(文本颜色/填充颜色/两者)。例如IF A1 = 1且B1 = 0那么格式B1,IF A2 = 1且B2 = 0那么格式B2 ... 在我的示例
中,我有A1和B1的值确定B1的格式:如果A#= 1 AND B#= 0那么FONT是红色的。 任何有关如何完成此任务的提示都非常感谢。                         
                                                                         
                                

I need a short excel macro to change the formatting of cells in a column (text color / fill color / both) based on the values of two cells in single row. For instance IF A1 = 1 and B1=0 THEN format B1, IF A2 = 1 and B2 = 0 THEN format B2...  In my example I have the values of A1 AND B1 determining the formatting for B1: IF A#=1 AND B#=0 THEN FONT IS RED.  Any tips on how to accomplish this are greatly appreciated.                                                                                                                                    

      A       B

     A     B

1 |  1      0 [RED]

1|  1     0[RED]

2 |  1      13 [BLACK]

2|  1     13[BLACK]

3 |  0      0 [BLACK]

3|  0     0[BLACK]

4 |  0      1 [BLACK]

4|  0     1[BLACK]

5 |  1      0 [RED]

5|  1     0[RED]

推荐答案

例如:

Sub FormatCells()

    Dim r As Long

    Dim m As Long

    Application.ScreenUpdating = False

    m =范围("A:B")。查找(内容:=" *",SearchOrder:= xlByRows,SearchDirection:= xlPrevious)。行¥b $ b   范围("B1:B"& m).Font.ColorIndex = xlAutomatic

   对于r = 1到m由
       如果范围("A"& r).Value = 1和范围("B"& r).Value = 0则为
       ;     范围("B"& r).Font.Color = vbRed

       结束如果是
   下一个r

    Application.ScreenUpdating = True

End Sub

Sub FormatCells()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("A:B").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("B1:B" & m).Font.ColorIndex = xlAutomatic
    For r = 1 To m
        If Range("A" & r).Value = 1 And Range("B" & r).Value = 0 Then
            Range("B" & r).Font.Color = vbRed
        End If
    Next r
    Application.ScreenUpdating = True
End Sub


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

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