简体   繁体   中英

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.

I have over 100 Worksheets that need styling, and the specific text is in different cells for each Worksheet.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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