查找并格式化excel宏

时间:2012-11-12 19:39:53

标签: excel excel-vba excel-2007 vba

我需要在excel中使用一种方法来浏览我的整个工作簿,找到所有带有匹配大小写的单词并将它们添加到斜体中。

我有像这样的数据的单元格:

Percentage of CTE concentrators who have met the proficient or advanced level on the 
statewide high school mathematics assessment administered by the state under ESEA and 
who, in the reporting year, left secondary education.

我需要将所有“ESEA”改为斜体。

有没有办法在Excel中执行此操作,还是需要宏?

1 个答案:

答案 0 :(得分:2)

以下是为您执行此操作的代码:

Sub Macro1()
Dim sFirstAddress As String, rgFound As Range
Const sSearch As String = "ESEA"

Set rgFound = Cells(1, 1)

Do While Not rgFound Is Nothing

    Set rgFound = Cells.Find(What:=sSearch, After:=rgFound, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, _
            SearchFormat:=False)

    If rgFound.Address = sFirstAddress Then Exit Do

    If InStr(rgFound.Value, sSearch) > 0 Then
        If Len(sFirstAddress) = 0 Then sFirstAddress = rgFound.Address
        rgFound.Characters(InStr(rgFound.Value, sSearch), Len(sSearch)).Font.FontStyle = "Italic"
    End If

Loop

End Sub