【问题标题】:Highlighting searched words on Vba excel在 Vba excel 上突出显示搜索的单词
【发布时间】:2016-02-17 21:44:10
【问题描述】:

我正在尝试为突出显示和搜索的单词添加计数,到目前为止,我将 icount 添加为字符串,它有时最多计数为 1,有时为 2,我认为我的公式可能是错误的,而且我的室友也擅长 c并认为我应该将 icount 作为字符串更改为长整数或整数。

Sub highlightext()

 Application.ScreenUpdating = False

 Dim ws As Worksheet
 Set ws = Worksheets("Sheet1")

 Dim oRange As Range
 Set oRange = ws.Cells


  Dim wordToFind As String
  wordToFind = InputBox(Prompt:="What word would you like to highlight?")

 Dim cellRange As Range
 Set cellRange = oRange.Find(What:=wordToFind, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows,  SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

If Not cellRange Is Nothing Then

Dim Foundat As String
Foundat = cellRange.Address
Set outws = Worksheets("product")
outws.Range("A2").Value = wordToFind


Do

    Dim textStart As Integer
    textStart = 1

    Do

        textStart = InStr(textStart, LCase(cellRange.Value), LCase(wordToFind))
        If textStart <> 0 Then
            cellRange.Characters(textStart, Len(wordToFind)).Font.Color = RGB(250, 0, 0)
            textStart = textStart + 1



        End If


    Loop Until textStart = 0


    Set cellRange = oRange.FindNext(After:=cellRange)

Loop Until cellRange Is Nothing Or cellRange.Address = Foundat

End If





 Dim icount() As String

 icount = Split(Foundat, ", ")

 outws.Range("B2").Value = UBound(icount) + 1



 End Sub

【问题讨论】:

  • 你需要通过逐行运行代码来调试这个[反复按f8]。我不知道你想用代码的 cellRange2 部分做什么,但我怀疑它正在做你想做的事。似乎循环的顺序不正确,或者其他什么。但是 - 要求我们做你自己没有做过的调试太过分了。
  • 在你的最后一个For each cell in oRange 循环中,将oRange 更改为Range(Foundat),你将是金色的(或者至少我的测试是金色的.. 或者是红色的 :))。您想遍历实际找到匹配项的地址范围,而不是再次遍历 A 列的整个范围。否则,您将按字面意思突出每个单元格的前 5 个字符,因为 wordToFind 未找到,因为 Instr 将为 0。
  • 您是否希望代码仅在单元格中为wordToFind第一个 实例着色??
  • 有点工作,如果单元格中有“wordtofind”,它会突出显示句子中的第一个单词,但不会突出显示单词本身,不会通读句子@ScottHoltzman
  • 这很有趣@Dayday。我在文本的开头/中间/结尾处使用了一个单词进行了测试,它按照您的要求将它们全部突出显示 - 只有 actual 单词被突出显示。

标签: vba excel


【解决方案1】:

下面是经过全面测试的代码和截图。

Sub highlightext()

Application.ScreenUpdating = False

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

Dim oRange As Range
Set oRange = ws.Range("A:A")

Dim wordToFind As String
wordToFind = InputBox(Prompt:="What word would you like to highlight?")

Dim cellRange As Range
Set cellRange = oRange.Find(What:=wordToFind, LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)

If Not cellRange Is Nothing Then

    Dim Foundat As String
    Foundat = cellRange.Address

    Do

        Dim textStart As Integer
        textStart = 1

        Do

            'to compare lower case only use this
            'textStart = InStr(textStart, LCase(cellRange.Value), LCase(wordToFind))
            textStart = InStr(textStart, cellRange.Value, wordToFind)
            If textStart <> 0 Then
                cellRange.Characters(textStart, Len(wordToFind)).Font.Color = RGB(250, 0, 0)
                textStart = textStart + 1
            End If


        Loop Until textStart = 0

        Set cellRange = oRange.FindNext(After:=cellRange)

    Loop Until cellRange Is Nothing Or cellRange.Address = Foundat

End If

End Sub

有一些陷阱可能会在单词中出现某些单词(例如Scottish 中的Scott,在我的示例中,或Scott's` 中的Scott)。也许这些是否适用于您,因此您可能需要进行一些调整。

【讨论】:

  • 非常感谢,我要稍微调整一下以添加一个计数器,显然有帮助
  • 我遇到了小写和大写的问题,所以我更改了你的 textstart
  • @Dayday - 请参阅Option Compare Text 处理大小写敏感性。这是另一个可能有用的link
  • 我尝试在不同的工作表上添加计数,以基本上计算突出显示的单词出现的次数。我将 icount 调暗为整数,我觉得我的循环是正确的,但是计数只上升到两个
  • @Dayday - 如果您仍然需要帮助,请发布一个新问题。
猜你喜欢
  • 2017-12-04
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 2011-12-20
相关资源
最近更新 更多