简体   繁体   English

VBA宏可在单元格范围内查找特定文本并将其设置为粗体

[英]VBA Macro to find specific text within cell range and style it bold

I am trying to develop a Macro to find specific text in all Worksheets within a Workbook and style the text bold. 我正在尝试开发一个宏,以在工作簿中的所有工作表中查找特定的文本,并将文本设置为粗体。

Here is what i have currently which works fine: 这是我目前可以正常工作的东西:

Sub Style_Worksheets()

Dim ws As Worksheet

For Each ws In Sheets
    ws.Activate

Dim sCellVal As String

sCellVal = Range("A1").Value
sCellVal = Range("A5").Value
sCellVal = Range("A7").Value
sCellVal = Range("B7").Value

If sCellVal Like "*Workflow Name:*" Or _
sCellVal Like "Events*" Or _
sCellVal Like "Event Name*" Or _
sCellVal Like "Tag File*" Then

Range("A1").Font.Bold = True
Range("A5").Font.Bold = True
Range("A7").Font.Bold = True
Range("B7").Font.Bold = True

End If
Next ws
End Sub

Now the problem I am currently facing is that I have specific text that in one Worksheet is in cell A16, but in another Worksheet is in A10. 现在,我当前面临的问题是,我有一个特定的文本,其中一个工作表位于单元格A16中,而另一个工作表位于A10中。

I have over 100 Worksheets that need styling, and the specific text is in different cells for each Worksheet. 我有100多个需要样式的工作表,每个工作表的特定文本位于不同的单元格中。

I would like the Macro to find specific text between cells A10 and A16 and if it finds the text, I want it to style it bold. 我希望宏在单元格A10和A16之间找到特定的文本,如果找到该文本,则希望将其设置为粗体。

I have tried adding the following into its relevant places: 我尝试将以下内容添加到其相关位置:

sCellVal = Range("A10:A16").Value

and: 和:

sCellVal Like "Workflow Level Mappings*" Or _

and: 和:

Range("A10:A16").Font.Bold = True

...but no joy. ...但是没有喜悦。

Can anyone help me out? 谁能帮我吗?

Thanks, 谢谢,

A 一种

Give this a shot. 试一下。 Fully tested. 经过全面测试。

Option Explicit

Sub Style_Worksheets()

    Dim TestPhrases() As String
    TestPhrases = Split("Workflow Name:,Events,Event Name,Tag File", ",")

    Dim ws As Worksheet

    For Each ws In Worksheets

        Dim CheckCell As Range
        For Each CheckCell In ws.Range("A10:A16")

            Dim Looper As Integer
            For Looper = LBound(TestPhrases) To UBound(TestPhrases)

                If InStr(CheckCell.Value, TestPhrases(Looper)) Then
                    CheckCell.Font.Bold = True
                    Exit For
                End If


            Next Looper

        Next CheckCell

    Next ws

End Sub

Just loop over the cells in question: 只需循环讨论有问题的单元格即可:

Sub Style_Worksheets()

    Dim ws As Worksheet, sCellVal As String
    Dim R As Range

    For Each ws In Sheets
        ws.Activate
        For Each R In Range("A1:A16")

            sCellVal = R.Text

            If sCellVal Like "*Workflow Name:*" Or _
                sCellVal Like "Events*" Or _
                sCellVal Like "Event Name*" Or _
                sCellVal Like "Tag File*" Then
                    R.Font.Bold = True
            End If
        Next R
    Next ws
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM