为宏找到某个单词的行着色

时间:2016-12-20 13:47:16

标签: excel vba excel-vba excel-formula

我想知道是否有办法使用VBA执行以下操作: 如果宏在列B中找到单词“Total”,那么total的行的colors.color将以蓝色着色,并对B列中的所有“Total”单词执行。 注意:我有不同的总数...它不仅仅是“总计”这个词

就像这样(即从col A到F的颜色)

enter image description here

我试过这个,但是它运行不正常且代码不好......

Sub forme_couleur()
Dim myRow As Integer

  myRow = 1

  While Not IsEmpty(Cells(myRow, 2))
    If Cells(myRow, 2).Find(What:="Total") Is Nothing Then
      myRow = myRow + 1
    Else
      Cells(myRow, 2).Find(What:="Total").Interior.Color = RGB(174, 240, 194)
    End If
    myRow = myRow + 1
  Wend
End Sub

5 个答案:

答案 0 :(得分:6)

考虑:

Sub ColorMeBlue()
    Dim i As Long, N As Long, s As String
    N = Cells(Rows.Count, "B").End(xlUp).Row
    s = "Total"

    For i = 1 To N
        If InStr(1, Cells(i, 2).Value, s) > 0 Then
            Range("A" & i & ":F" & i).Interior.Color = RGB(174, 240, 194)
        End If
    Next i
End Sub

修改#1:

要使用数字列来引用范围,请使用:

Sub ColorMeBlue2()
    Dim i As Long, N As Long, s As String
    N = Cells(Rows.Count, "B").End(xlUp).Row
    s = "Total"
    Firstcol = 1
    LastCol = 6
    For i = 1 To N
        If InStr(1, Cells(i, 2).Value, s) > 0 Then
            Range(Cells(i, Firstcol), Cells(i, LastCol)).Interior.Color = RGB(174, 240, 194)
        End If
    Next i
End Sub

答案 1 :(得分:2)

您可以使用条件格式来实现此目的,但如果必须使用VBA,请使用以下内容:

Sub test()

For i = 1 To Cells(ActiveSheet.Rows.Count, "B").End(xlUp).Row
If InStr(1, Cells(i, 2), "Total") Then
    With Cells(i, 2).EntireRow.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With
End If
Next i
End Sub

答案 2 :(得分:1)

另一个概念:您可以使用AutoFilter方法。使用此方法不需要任何For循环或任何If s,只需使用通过范围内AutoFilter ="*Total*"条件的所有单元格。

Sub ColorMeBlue_Filter()

Dim i As Long, N As Long, s As String
Dim FirstCol As Long, LastCol As Long
Dim FiltRng As Range

N = Cells(Rows.Count, "B").End(xlUp).Row
s = "Total"

' (just for my testing)
'FirstCol = 1
'LastCol = 6

Range("A1").AutoFilter
Range(Cells(1, FirstCol), Cells(N, LastCol)).AutoFilter Field:=2, Criteria1:="=*Total*", _
        Operator:=xlAnd

' set FiltRng to only visible cells (that passed the "Total" filter)
Set FiltRng = Range(Cells(2, FirstCol), Cells(N, LastCol)).SpecialCells(xlCellTypeVisible)

' modify interior color of all cells at once (one code line)
FiltRng.Interior.Color = RGB(174, 240, 194)

End Sub

答案 3 :(得分:0)

您可以使用基于公式的条件格式,使用COUNTIF(a1:f1,“总计”)函数> 0

答案 4 :(得分:0)

使用Range.Find避免:遍历每一行并需要获取最后一行。

不是将Range.Find应用于每一行,只需将其应用于整个列,无需检查该单元格是否为空(有关其他详细信息,请参阅Range.Find Method (Excel)

Voici votrecoderévisé:

假设您的数据位于“A:F”

Sub forme_couleur()
Const kCriteria As String = "Total"
Dim rTrg As Range, s1stFound As String
    With ThisWorkbook.Sheets("DATA").Columns(2)     'change as required
        Set rTrg = .Cells.Find(What:=kCriteria, After:=.Cells(1), LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        If Not (rTrg Is Nothing) Then
            s1stFound = rTrg.Address
            Do
                rTrg.EntireRow.Cells(1).Resize(1, 6).Interior.Color = RGB(224, 240, 248)    'RGB(174, 240, 194) give me a green color - changed as required
                Set rTrg = .Cells.FindNext(After:=rTrg)
            Loop Until rTrg.Address = s1stFound
    End If: End With
    End Sub