如何在VBA中突出显示包含特定关键字的行?

时间:2014-05-06 05:35:20

标签: excel-vba excel-2010 vba excel

我正在使用MS Excel 2010 VBA.i想要编写一个代码,用于突出显示包含关键字canada的行,我已编写以下代码,

       Cells.Find(What:="Canada", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows,         SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
      ActiveCell.EntireRow.Select
      With Selection.Font
             .Color = -16776961
             .TintAndShade = 0
      End With

我想在循环中使用代码来突出显示包含关键字" canada"的所有行。如何使用VBA执行此操作?

1 个答案:

答案 0 :(得分:0)

示例:

Sub Highlight()
    Dim vCell As Range

    'Loop through every used cell in the active worksheet
    For Each vCell In ActiveSheet.UsedRange
        'If the cells value contains "Canada" change its font to red
        If InStr(vCell.Value, "Canada") Then
            vCell.Font.Color = -16776961
        End If
    Next
End Sub

这是微软办公网站的链接,解释循环如何工作:
http://office.microsoft.com/en-us/training/get-in-the-loop-with-excel-macros-RZ001150634.aspx?section=11