Excel VBA根据不同单元格中的值填充单元格颜色

时间:2016-10-24 13:54:41

标签: excel vba excel-vba

如果值= 0且列L中的相邻单元格值为空,我希望修改我的代码以仅填充红色单元格。

对于值为0且单元格L中的相邻单元格值不为空的单元格,我想用模式填充0单元格。

我附上了它的外观截图。

enter image description here

                              Table "public.a"
 Column |       Type        |                   Modifiers                    
--------+-------------------+------------------------------------------------
 id     | bigint            | not null default nextval('a_id_seq'::regclass)
 bar    | character varying | 
Indexes:
    "a_pkey" PRIMARY KEY, btree (id)
    "ix_a_bar" btree (bar)
Referenced by:
    TABLE "b" CONSTRAINT "b_a_id_fkey" FOREIGN KEY (a_id) REFERENCES a(id)

                                      Table "public.b"
   Column   |            Type             |                    Modifiers                     
------------+-----------------------------+--------------------------------------------------
 id         | bigint                      | not null default nextval('b_id_seq'::regclass)
 foo        | character varying           | 
 a_id       | bigint                      | not null
 created_at | timestamp without time zone | 
Indexes:
    "b_pkey" PRIMARY KEY, btree (id)
    "ix_b_created_at" btree (created_at)
    "ix_b_foo" btree (foo)
Foreign-key constraints:
    "b_a_id_fkey" FOREIGN KEY (a_id) REFERENCES a(id)

3 个答案:

答案 0 :(得分:1)

你很亲密!我会尝试接受你的代码并稍微修改一下

Sub Fill_Cell()

Dim rngCell As Range

For Each rngCell In Range("C7:K100")
    If rngCell.Value = "0" Then
        If Cells(rngCell.Row, 12).Value <> "" Then
            'If it is 0 and L not empty
            rngCell.Cells.Interior.Pattern = xlGray16
        else
            'If it is 0 and L is empty
            rngCell.Cells.Interior.ColorIndex = 3
        End If
    End If
Next

End Sub

答案 1 :(得分:1)

试试这个

Option Explicit

Sub Fill_Cell()

Dim rngCell As Range

For Each rngCell In Range("C7:K100")
    If rngCell.Value = "0" Then
        Select Case Cells(rngCell.row, "L")
            Case Is = ""
                rngCell.Cells.Interior.ColorIndex = 3
            Case Else
                rngCell.Cells.Interior.Pattern = xlGray16
        End Select
    End If
Next rngCell

End Sub

答案 2 :(得分:1)

似乎有几个人打败了我:

   For Each rngCell In Range("C7:K100")
        If rngCell.Value = "0" And Cells((rngCell.Row), 12).Value <> "" Then
            rngCell.Cells.Interior.Pattern = xlGray16
        ElseIf rngCell.Value = "0" Then
            rngCell.Cells.Interior.ColorIndex = 3
        End If
    Next
相关问题