根据列值VBA更改整行的内部颜色

时间:2017-08-30 19:17:25

标签: excel vba excel-vba

需要插入一条声明,当VDB(i,35)=删除时,整个行内部颜色变为颜色索引22(珊瑚色)。

我需要在此块中执行此步骤,而无需在新块中添加代码(除非绝对必要),因为工作表具有20K以上的条目。我假设,因为这是我在识别项目状态为"已删除"以及放置"已删除"在第35栏中,我应该能够在同一个步骤/块中对其进行着色,这将是最有效的方法。我可能错了..

我可以在最后一行之后添加另一行,它会为这些条目着色="已删除"在col指数35?

我已经尝试将vDB(i,35)传递给另一个变量作为范围,并设置它,然后使用if if = Deleted来更改wholerow.interior.color index = 22,但我不能得到正确的措辞,并可能采取错误的方法。我还在学习曲线,但在开始讨论小组之前,我试着弄清楚自己的问题,但我似乎无法做到正确。

这是剪辑它。

'Execute Find (Vlookup)

For i = 1 To UBound(vDB, 1)
'If sht.Cells(i, 1).Value <> "" Then
    If vDB(i, 1) <> "" Then
        Set rng = rngData.Find(vDB(i, 1), LookIn:=xlValues, Lookat:=xlWhole) 'Matches entire contents of cell for an exact match
        If Not rng Is Nothing Then
           'If found return value of column 9 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix monthly ABC Code column, as determined by variable
            vDB(i, ABCCodeCell) = rng.Offset(, 7)
            'If found, return the value of column 7 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 27
            vDB(i, 27) = rng.Offset(, 5)
            'If found, return the value of column 11 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 34
            vDB(i, 33) = rng.Offset(, 9)
            'If found, place value of ABCMatrixMonthSelect.ComboBox1 in column AO Col Index 41
            vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value
        Else
            vDB(i, 35) = "Deleted"
            vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value
            With vDB(i, 1) = sht.Cells.Interior.Color = RGB(247, 119, 109) 'Light Red
            End With
        End If
    End If
    If vDB(i, ABCCodeCell) = vDB(i, lastMonthABCCode) Then
    vDB(i, 36) = "No"
    Else
    vDB(i, 36) = "Yes"
    End If
DoEvents
Next
rngDB = vDB

Dim LR As Long
LR = sht.Cells(Rows.Count, 1).End(xlUp).Row
sht.Cells.FormatConditions.Delete
With sht.Range("1:" & LR)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Searches for value "Deleted" in Range 1 to last row
    With .FormatConditions(.FormatConditions.Count)
       .SetFirstPriority
       With .Interior
           .Color = RGB(247, 119, 109) 'Light Red
       End With
    End With
End With

'Reset Excel Status Bar
Application.StatusBar = False
e here

1 个答案:

答案 0 :(得分:0)

如果您可以使用条件格式,请使用规则类型:使用公式确定要格式化的单元格,在公式栏中键入 = $ AJ35 =“已删除” ,然后定义您的范围,例如 = 1:1000 。 $在公式中的位置是关键。然后,您将选择满足条件时发生的情况并设置规则。

您可以从VBA执行此操作,设置条件格式,例如:

Dim LR as Integer
LR = Cells(Rows.Count, 1).End(xlUp).Row 'just finding the row # of the last row
sht.Cells.FormatConditions.Delete
With sht.Range("1:" & LR) 'The range you're working with... specify the Rows() to ensure the whole row is colored 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Where you put the cell you want to evaluate and criteria
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .Color = RGB(0, 70, 255) 'Arbitrary; you will need to find the specific color you want.
            .TintAndShade = 0.8
        End With
    End With
End With

请注意,我每次都会删除现有条件。

此外,另一种选择就是说,它不使用条件格式:

With sht.Rows(i).Interior
    .Color=RGB(0,255,0) 'arbitrary color
    .TintAndShade = 0.8
End With

编辑:使用经过调整的代码并希望对第1列到第35列进行颜色编码:

Else
    vDB(i, 35).Value = "Deleted"
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
        .Color = RGB(247, 167, 184) 'light red
        .TintAndShade = 0.8
    End With
End If

我在想你的vDB有什么用,所以也许使用Cell更合适?

Else
    Cells(i, 35).Value = "Deleted"
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
        .Color = RGB(247, 167, 184) 'light red
        .TintAndShade = 0.8
    End With
End If

同样:

Dim i, LR as Integer
LR = Cells(Rows.Count,1).End(xlUp).Row 'Assumes row 1 is contiguous... probably not, given the rest of this code
For i = 1 To LR
    If Cells(i, 1) <> "" Then
        Set rng = rngData.Find(Cells(i, 1), LookIn:=xlValues, Lookat:=xlWhole)
        If Not rng Is Nothing Then
            Cells(i, ABCCodeCell).Value = rng.Offset(, 7).Value
            Cells(i, 27).Value = rng.Offset(, 5).Value
            Cells(i, 34).Value = rng.Offset(, 9).Value
        Else
            Cells(i, 35).Value = "Deleted"
            With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
                .Color = RGB(247, 167, 184) 'light red
                .TintAndShade = 0.8
            End With