簡體   English   中英

VBA宏可在單元格范圍內查找特定文本並將其設置為粗體

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

我正在嘗試開發一個宏,以在工作簿中的所有工作表中查找特定的文本,並將文本設置為粗體。

這是我目前可以正常工作的東西:

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

現在,我當前面臨的問題是,我有一個特定的文本,其中一個工作表位於單元格A16中,而另一個工作表位於A10中。

我有100多個需要樣式的工作表,每個工作表的特定文本位於不同的單元格中。

我希望宏在單元格A10和A16之間找到特定的文本,如果找到該文本,則希望將其設置為粗體。

我嘗試將以下內容添加到其相關位置:

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

和:

sCellVal Like "Workflow Level Mappings*" Or _

和:

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

...但是沒有喜悅。

誰能幫我嗎?

謝謝,

一種

試一下。 經過全面測試。

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

只需循環討論有問題的單元格即可:

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