【问题标题】:VBA excel how to fix the code (exclude rows that are empty ) do not fill the background in a colorVBA excel如何修复代码(排除空行)不填充颜色的背景
【发布时间】:2017-11-06 10:32:28
【问题描述】:

我有一个用 VBA 编写的代码,它将检查日期并根据它用适当的颜色填充背景。

我有单元格(A 到 G)。

我想检查C列是否为空我想保持行透明

  If IsEmpty(Cells(i, 3)) Then
         Range("A" & i & ":G" & i).Interior.Color = xlNone

问题是代码进入if语句检查是否为空......然后它进入最后一个if语句并用指定的颜色填充所有空行。

代码:

Private Sub CommandButton1_Click()

Dim i As Long

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
         Range("A" & i & ":G" & i).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

    ' your 2nd criteria to color the entire row if "G" is not empty
    If Trim(Range("G" & i).Value) <> "" Then
                       Range("A" & i & ":G" & i).Interior.ColorIndex = 15


  ElseIf Trim(Range("G" & i).Value) = "" Then
                       Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
                       Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)

 End If

Next
End Sub

【问题讨论】:

  • 您的代码 cmets 提到“如果“F”不为空,您的第二个标准为整行着色”但代码实际上是指 G 列。这是故意的吗?
  • 您的意思是如果 C 为空,则无论 G 是否为空,该行都应该​​是“透明的”?
  • @SJR YESSSS 其他列是否为空无关紧要
  • @Moacir 这是 cmets 的一个错误,谢谢

标签: excel for-loop if-statement vba


【解决方案1】:

试试这个,只是在你的第二个语句中添加了一个额外的 If 子句

Private Sub CommandButton1_Click()

Dim i As Long

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
        Range("A" & i & ":G" & i).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

    ' your 2nd criteria to color the entire row if "G" is not empty
    If Not IsEmpty(Cells(i, 3)) Then
        If Trim(Range("G" & i).Value) <> "" Then
            Range("A" & i & ":G" & i).Interior.ColorIndex = 15
        ElseIf Trim(Range("G" & i).Value) = "" Then
            Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
            Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
        End If
    End If
Next i

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2020-09-16
    • 2021-05-24
    相关资源
    最近更新 更多