当前位置: 动力学知识库 > 问答 > 编程问答 >

excel vba - Highlighting an entire row when condition is met

问题描述:

I am trying to highlight an enitre row when a condition is met. I am successfully able to highlight the cell with the word in it but having trouble highlighting the row. Blow is the code I have so far:

Sub Tester()

Dim rng As Range

Set rng = FindAll(Sheets("Current Tasks").Range("A:P"), "Completed")

If Not rng Is Nothing Then

rng.Rows(ActiveCell.Row).Interior.Color = 65535

'rng.Interior.Color = 65535

End If

End Sub

Public Function FindAll(rng As Range, val As String) As Range

Dim rv As Range, f As Range

Dim addr As String

Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.Count), _

LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _

SearchDirection:=xlNext, MatchCase:=False)

If Not f Is Nothing Then addr = f.Address()

Do Until f Is Nothing

If rv Is Nothing Then

Set rv = f

Else

Set rv = Application.Union(rv, f)

End If

Set f = rng.FindNext(after:=f)

If f.Address() = addr Then Exit Do

Loop

Set FindAll = rv

End Function

网友答案:

You can slightly modify your code as follows:

Sub Tester()
    Dim rng As Range

    Set rng = FindAll(Sheets("Current Tasks").Range("A:P"), "Completed")
    If Not rng Is Nothing Then
        rng.EntireRow.Interior.Color = 65535
    End If

End Sub
分享给朋友:
您可能感兴趣的文章:
随机阅读: