使用VBA选择并突出显示Excel行

Dan*_*Dan 4 excel vba rows highlight excel-vba

如何告诉Excel按行号突出显示行.例如,假设我想要突出显示第6,10,150,201行.谢谢.

Tim*_*ams 5

作为Motes答案的替代方案,您可以使用条件格式.

例如:选择A1:J500,条件格式>>新规则>>使用公式...

对于公式输入: =OR(ROW()=6, ROW()=10, ROW()=150, ROW()=201)


Sid*_*out 5

这是另一个基于Mote的 .EntireRow.Interior.ColorIndex

这个不限制您输入行号,但为用户提供了在运行时选择行的灵活性.

Option Explicit

Sub Sample()
    Dim Ret As Range

    On Error Resume Next
    Set Ret = Application.InputBox("Please select the rows that you would like to color", "Color Rows", Type:=8)
    On Error GoTo 0

    If Not Ret Is Nothing Then Ret.EntireRow.Interior.ColorIndex = 6
End Sub
Run Code Online (Sandbox Code Playgroud)

跟进

有没有办法编写宏来从列表中读取行号并突出显示行?

是的,有办法.假设单元格A1到A10中的列表,那么您可以使用此代码

Option Explicit

Sub Sample()
    Dim i As Long, sh As Worksheet

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    '~~> Set this to the sheet where the rows need to be colored
    Set sh = Sheets("Sheet2")

    '~~> Change Sheet1 to the sheet which has the list
    With Sheets("Sheet1")
        For i = 1 To 10
            If Not Len(Trim(.Range("A" & i).Value)) = 0 And _
            IsNumeric(.Range("A" & i).Value) Then _
            sh.Rows(.Range("A" & i).Value).Interior.ColorIndex = 3 '<~~ Red
        Next i
    End With

LetsContinue:
    Application.ScreenUpdating = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
Run Code Online (Sandbox Code Playgroud)