【问题标题】:Hightlighting keywords in one worksheet from a list found in a second worksheet从第二个工作表中找到的列表中突出显示一个工作表中的关键字
【发布时间】:2013-03-18 15:22:59
【问题描述】:

我已经尝试了一段时间。我快到了,但我的 vba 技能还没有完成。我在工作表中有一些文本,我需要搜索并找到一些关键字的所有实例。

理想情况下,我会将关键字放在第二个工作表的某个范围内。我想不通。我已经能够使用以下内容搜索固定数组,但无法弄清楚如何将其带到下一步从工作表中获取单词。

Sub X()

Dim vntWords As Variant
Dim lngIndex As Long
Dim rngFind As Range
Dim strFirstAddress As String
Dim lngPos As Long

vntWords = Array("sales", "job")

With ActiveSheet.UsedRange
    For lngIndex = LBound(vntWords) To UBound(vntWords)
        Set rngFind = .Find(vntWords(lngIndex), LookIn:=xlValues, lookat:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstAddress = rngFind.Address
            Do
                lngPos = 0
                Do
                    lngPos = InStr(lngPos + 1, rngFind.Value, vntWords(lngIndex), vbTextCompare)
                    If lngPos > 0 Then
                        With rngFind.Characters(lngPos, Len(vntWords(lngIndex)))
                            .Font.Bold = True
                            .Font.Size = .Font.Size
                            .Font.ColorIndex = 3
                        End With
                    End If
                Loop While lngPos > 0
                Set rngFind = .FindNext(rngFind)
            Loop While rngFind.Address <> strFirstAddress
        End If
    Next
End With

结束子

【问题讨论】:

    标签: excel highlight


    【解决方案1】:

    可能对你有帮助

    ThisWorkbook.Sheets("name of sheet with search terms").Range("A1:A")
    

    这会将A 列中的所有值作为数组提供。您可以在代码中使用。

    【讨论】:

    • 我用 vntWords = ThisWorkbook.Sheets("Keywords").Range("A1:A") 替换了 vntWords = Array("sales", "job") 并得到了一个“运行时错误 1004 应用程序定义的或对象定义的错误”,所以这似乎不起作用。
    • 在这种情况下我试图按名称获取工作表,因此请尝试使用最新的 VBA 语法 Worksheets("Keywords").Range("A1:A")
    猜你喜欢
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 2014-06-12
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多