vba excel 如何检查单元格是否为空并为整行着色?

vba excel how to check if cell not empty and color the entire row?

我有 VBA 代码,可以将用户输入的日期与当前日期进行比较,并用适当的颜色填充背景。一切正常。

现在我需要让系统检查所选行 F 列中的单元格是否不为空 我需要将列 D,E,F 涂成灰色。

代码:

Private Sub CommandButton1_Click()

    Dim i As Integer

    For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'

        If IsEmpty(Cells(i, 3)) Then
            Cells(i, 3).Interior.Color = xlNone

        ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
            Cells(i, 3).Interior.Color = vbGreen

        ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
            Cells(i, 3).Interior.Color = vbYellow

        ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
            Cells(i, 3).Interior.Color = vbRed

        ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
            Cells(i, 3).Interior.Color = vbCyan

        Else
            Cells(i, 3).Interior.ColorIndex = xlNone

        End If

    Next

End Sub

我将不胜感激任何帮助

您还需要检查相关标准中是否 Trim(Range("F" & i).Value) <> ""

我还通过(切换到 Select Case 等)

修改了您当前代码的逻辑,使其更短更快 运行

代码

Private Sub CommandButton1_Click()

Dim i As Long
Dim NumofDays As Long

For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
    Cells(i, 3).Interior.Color = xlNone ' set as inital color, only change if all criterias are met

    NumofDays = CDate(Cells(i, 3)) - Date

    Select Case NumofDays
        Case Is < 0
            Cells(i, 3).Interior.Color = vbGreen

        Case 0
            Cells(i, 3).Interior.Color = vbYellow

        Case 1 To 4
            Cells(i, 3).Interior.Color = vbRed

        Case 5 To 10
            Cells(i, 3).Interior.Color = vbCyan

    End Select

    ' your 2nd criteria to color the entire row if "F" is not empty
    If Trim(Range("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.Color = ... ' selectyourcolor 
Next i

End Sub

条件格式最简单,只需a few clicks

  1. Start with selecting the cells the background color of which you want to change.
  2. Create a new formatting rule by clicking Conditional Formatting > New Rule... on the Home tab.
  3. On the Home tab, click Conditional Formatting > New Rule... In the "New Formatting Rule" dialog window that opens, choose the option "Use a formula to determine which cells to format" and enter the following formula in the "Format values where this formula is true" field: =$C2>4
  4. Open the 'New Formatting Rule' dialog and enter the needed formula. Instead of C2, you enter a cell that contains the value you want to check in your table and put the number you need instead of 4. And naturally, you can use the less (<) or equality (=) sign so that your formulas will read =$C2<4 and =$C2=4, respectively.
  5. Also, pay attention to the dollar sign $ before the cell's address, you need to use it to keep the column letter the same when the formula gets copied across the row. Actually, it is what makes the trick and applies formatting to the whole row based on a value in a given cell.
  6. Click the "Format..." button and switch to Fill tab to choose the background color. If the default colors do not suffice, click the "More Colors..." button to pick the one to your liking, and then click OK twice.