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

标签 vba excel

我正在尝试开发一个宏来查找工作簿内所有工作表中的特定文本并将文本设置为粗体。

这是我目前的工作正常:

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

关于VBA宏在单元格范围内查找特定文本并将其设置为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47038816/

相关文章:

java - Apache POI 和文件删除类

vb.net - 无法设置 PageSetup 类的 PaperSize 属性

ms-access - 在 Access 时间格式中自定义文本框

vba - 引用不在默认收件箱下的文件夹

variables - 通过变量引用工作簿和工作表

javascript - excel-JS excel left() 工作表函数不起作用

Excel VBA 使用 CDO 添加附件时出错。消息 "Object doesn' t 支持此属性或方法"

vba - 使用 VBA 将选择添加到命名范围?

excel-vba - 麻烦更改与vba的odbc连接的commandText

vba - 没有 .Display 的电子邮件正文为空