无法使此脚本工作

时间:2017-05-13 00:32:15

标签: vba

我试图找到所有粗体,然后更改其格式,所以我试图用两个循环一个用于查找粗体字,然后其他用于更改格式。有人可以告诉我该怎么做/任何帮助非常感谢。 “谢谢你

Sub SearchBoldText()
    Dim rng As Range
    Set rng = ThisDocument.Range(0, 0)
    With rng.Find
        .ClearFormatting
        .Format = True
        .Font.Bold = True


        While .Execute
            rng.Select
            rng.Collapse direction:=wdCollapseEnd
        Wend

        Do Until rng = 0
                With Selection.Font
                    .Name = "Times New Roman"
                    .Size = 20
                    .Bold = True
                    .Color = RGB(200, 200, 0)
                End With
                Selection.Find.Execute
            Loop
        End With
    End With
    Set rng = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

这件事不需要2个循环。假设您的范围是A1到F40,请执行以下操作。请注意,我没有更改格式,但我已将单元格的背景设置为红色。你可以根据自己的需要进行调整。

对于EXCEL:

Sub SelectBold()
    Dim Rng As Range
    Dim WorkRng As Range

    Set WorkRng = Range("A1:F40")

    For Each Rng In WorkRng
        If Rng.Font.Bold Then
            Rng.Interior.Color = RGB(255, 0, 0)
        End If
    Next
End Sub

对于WORD:

Sub findBold()
    Selection.Find.ClearFormatting
    Selection.Find.Font.Bold = True
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Size = 20
    Selection.Find.Replacement.Font.ColorIndex = wdYellow
    Selection.Find.Replacement.Font.Name = "Times New Roman"
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub