通过搜索A:A中的特定文本为列着色

时间:2017-03-28 06:08:13

标签: excel vba excel-vba

通过搜索特定文字,在A列中帮助我使用VBA来对色谱柱进行着色,在A栏中说“英国'” enter image description here

3 个答案:

答案 0 :(得分:0)

一个非常简短,肮脏和粗鲁的代码如下:

Sub ColorColumns()
    On Error Resume Next
    Range("A1", Cells(Rows.Count, 1).End(xlUp)).Find(what:=Application.InputBox("Text to search:", , , , , , , 2), LookIn:=xlValues, lookat:=xlWhole).Resize(, 7).Interior.ColorIndex = 6
End Sub

答案 1 :(得分:0)

您可以使用以下代码。记住在运行代码时保持包含表的工作表处于活动状态。或者,您可以在设置ws变量时明确指定工作表。

Sub SelectivelyColorARowRed()

    Dim ws As Worksheet
    Set ws = ActiveSheet
    lng_lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim k As Long

    For k = 2 To lng_lastrow
        If Trim(ws.Cells(k, 1).Value) = "UK" Then
        'you can also apply UCase if you want the search to be case-insensitive
        'like: If UCase(Trim(ws.Cells(k, 1).Value)) = UCase("Uk") Then
            ws.Range(Cells(k, 1), Cells(k, 7)).Interior.ColorIndex = 22
        End If
    Next k

End Sub

答案 2 :(得分:0)

试试这个。 Code1适用于工作簿中的每个工作表,但如果您希望这个工作表适用于活动工作表,请尝试Code2。对于Code2,请记住写出正确的工作表名称。希望它有所帮助。

您可以在此处选择索引颜色:https://stackoverflow.com/a/25000926/7238313

代码1:

Sub rowhighlight()
Dim sht As Worksheet
Dim nlast As Long

For Each sht In ActiveWorkbook.Worksheets
    sht.Select
    nlast = Cells(Rows.Count, "A").End(xlUp).Row

        For n = nlast To 2 Step -1
            If sht.Cells(n, "A").Value = "UK" Then
                sht.Range("A" & n, "G" & n).Interior.ColorIndex = 37
                'different color number place here----------------^
            End If
        Next n
     Next sht
 End Sub

Code2:

Sub rowhighlight()
Dim nlast As Long

Sheets("sheetname").Activate
Set sht = ActiveWorkbook.ActiveSheet
nlast = Cells(Rows.Count, "A").End(xlUp).Row

    For n = nlast To 2 Step -1
        If sht.Cells(n, "A").Value = "UK" Then
            sht.Range("A" & n, "G" & n).Interior.ColorIndex = 37
            'different color number place here----------------^
        End If
    Next n
End Sub