如果匹配项突出显示值 VBA,则识别单词

2024-03-22

样本数据:

Code:

Sub HighlightMatchingWords()
    Dim lastRowSheet1 As Long
    Dim lastRowSheet2 As Long
    Dim i As Long
    Dim j As Long
    Dim cellValueSheet1 As String
    Dim cellValueSheet2 As String
    Dim wordsSheet1 As Variant
    Dim wordsSheet2 As Variant
    Dim wordIndexSheet1 As Long
    Dim wordIndexSheet2 As Long
    Dim wordSheet1 As String
    Dim wordSheet2 As String
    
    ' Get the last row of data in column A for Sheet1
    lastRowSheet1 = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Row
    
    ' Get the last row of data in column A for Sheet2
    lastRowSheet2 = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, 1).End(xlUp).Row
    
    ' Loop through each row of data in column A for Sheet1
    For i = 1 To lastRowSheet1
        ' Get the value in column A for the current row in Sheet1
        cellValueSheet1 = Sheets("Sheet1").Cells(i, 1).Value
    
        ' Split the string into words for Sheet1
        wordsSheet1 = Split(cellValueSheet1, " ")
    
        ' Loop through each row of data in column A for Sheet2
        For j = 1 To lastRowSheet2
            ' Get the value in column A for the current row in Sheet2
            cellValueSheet2 = Sheets("Sheet2").Cells(j, 1).Value
    
            ' Split the string into words for Sheet2
            wordsSheet2 = Split(cellValueSheet2, " ")
    
            ' Loop through each word in Sheet1
            For wordIndexSheet1 = 0 To UBound(wordsSheet1)
                ' Loop through each word in Sheet2
                For wordIndexSheet2 = 0 To UBound(wordsSheet2)
                    ' If the words match, highlight the word in Sheet1
                    If StrComp(wordsSheet1(wordIndexSheet1), wordsSheet2(wordIndexSheet2), vbTextCompare) = 0 Then
                        wordSheet1 = wordsSheet1(wordIndexSheet1)
                        ' Highlight the word in Sheet1
                        Sheets("Sheet1").Cells(i, 1).Characters(InStr(cellValueSheet1, wordSheet1), Len(wordSheet1)).Font.ColorIndex = 3 ' Highlight in red
                        Sheets("Sheet1").Cells(i, 2).Value = Sheets("Sheet1").Cells(i, 2).Value & " " & word
                    End If
                Next wordIndexSheet2
            Next wordIndexSheet1
        Next j
    Next i
End Sub

我试过了,但没有达到 100% 的准确率,有人可以帮忙吗?


这是实现此目的的一种方法.Find。我已经对代码进行了注释,因此您理解它应该不会有问题。如果您这样做,只需询问即可。

Code:

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim aCell As Range, bCell As Range
    Dim InputAr As Variant
    Dim i As Long     
    
    '~~> Set this to the relevant sheet
    Set ws = Sheet1
    
    '~~> This is the range where the text nees to be colored
    Set rng = ws.Range("A1:A6")
    
    '~~> This is the range where you have the keywords
    InputAr = ws.Range("D1:D5")
    
    '~~> Loop through the seach keywords
    For i = LBound(InputAr) To UBound(InputAr)
        '~~> Find the text
        Set aCell = rng.Find(What:=InputAr(i, 1), LookIn:=xlFormulas, LookAt:=xlPart)
        
        '~~> If found
        If Not aCell Is Nothing Then
            Set bCell = aCell
            
            '~~> Color the text
            ColorText aCell, InputAr(i, 1)
            
            '~~> Find the next occurance
            Do
                Set aCell = rng.FindNext(After:=aCell)
                
                If Not aCell Is Nothing Then
                    If aCell = bCell Then Exit Do
                    ColorText aCell, InputAr(i, 1)
                End If
            Loop
            
            '~~> Set the found range to Nothing
            Set aCell = Nothing
        End If
    Next i
End Sub

'~~> Proc to color the text
Private Sub ColorText(r As Range, keyword As Variant)
    Dim sPos As Long
    Dim TxtLen As Long
    
    '~~> Set the starting position
    sPos = InStr(1, r.Value2, keyword, vbTextCompare)
    '~~> Get the length
    TxtLen = Len(keyword)
    '~~> Color the text
    r.Characters(Start:=sPos, Length:=TxtLen).Font.Color = RGB(255, 0, 0)
End Sub

截屏:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果匹配项突出显示值 VBA,则识别单词 的相关文章

  • Range.End() 困惑

    我有一个关于 VBA 中 Range End 属性的一般性问题 我已经阅读了有关该房产的信息here http msdn microsoft com en us library bb221181 aspx 但我还是很困惑 例子 With w
  • 使用 Cucumber Scenario Outline 处理 Excel 电子表格

    如果可能的话 我试图找到一种更优雅的方法来处理从与 Excel 电子表格行 第 n 个 相关的 Cucumber Scenario Outline 中调用第 n 个数字 目前 我正在使用迭代编号来定义要从中提取数据的 Excel 电子表格的
  • SSIS使用列位置而不是名称导入Excel文档

    我想知道是否可以通过按位置引用列来使用 SSIS 导入 Excel 文档 例如 导入列 A D M AA 等 我问这个问题是因为我需要从第三方加载多个 Excel 文档 每个文档在相应的列中包含相同的数据类型 但每个文档的列名称不同 Tha
  • 拆分具有多行文本和单行文本的行

    我试图弄清楚如何拆分数据行 其中行中的 B C D 列包含多行 而其他列不包含多行 我已经弄清楚如何拆分多行单元格 如果我将这些列复制到新工作表中 手动插入行 然后运行下面的宏 仅适用于 A 列 但我在编码时迷失了休息 Here s wha
  • 需要在Excel中合并3列

    我有 3 列 A B C 我需要合并这 3 列 并且我已经应用了 forumala A1 B1 C1输出为 E 列 我需要输出为 D 列 下面的公式将达到您想要的结果 TEXTJOIN TRUE A1 C1 Textjoin 的工作方式类似
  • 调用退出后应用程序未退出

    我有一个小问题 我似乎无法弄清楚 我正在将 DataGridView 它的内容 保存到 xls 文件中 我这样做没有任何问题 除了在我的任务管理器中它仍然显示它正在运行 我已致电 xlApp Application Quit 这被声明为 D
  • 具有多个条件(全部等于相同值)的 IF 语句的替代方案

    还有比这更好的方法吗 假设所有变量都为零 If var1 0 Or var2 0 Or var3 0 Or var4 0 Or var5 0 Or var6 0 Then do something End If 你可以用Select Cas
  • 强力查询历年产品利润对比

    我有一个数据集 其中包含公司 产品 利润和年份 公司每年都会销售少量产品并获得利润 公司没有必要在明年销售相同的产品 他们可能会省略以前的产品并添加新的少量产品 我只想对两年的产品进行逐个比较 如下所示 我的数据集是 Company Pro
  • 如何将 HTML 表格导出为 .xlsx 文件

    我有一个关于导出的问题HTML表格 as an xlsx文件 我做了一些工作 现在我可以将其导出为xls 但我需要将其导出为xlsx 这是我的 jsFiddle https jsfiddle net 272406sv 1 https jsf
  • 使用 MemoryStream 创建 Open XML 电子表格时的 Excel 和“不可读内容”

    使用 Open XML SDK v2 0 创建 Excel 电子表格时 我们的 Excel 输出最初可以成功运行几个月 最近Excel 所有版本 开始抱怨 Excel在 zot xlsx 中发现不可读的内容 是否要恢复此工作簿的内容 我们正
  • Excel 工作表到 iPhone 数据 -- A 点到 B 点

    尽可能简单 我有一个非常简单的 Excel 电子表格 只有 1000 多条记录 我想将其用作 iPhone 应用程序的静态数据源 最好的进攻计划是什么 我心中的可能性 1 直接读取XLS作为数据源 是否有Obj C库用于此 2 将XLS 转
  • Excel VBA - 如何逐行读取csv文件而不是整个文件

    这是我需要读取的 csv 文件内容 header header header header header header value value value value value value value value value 我在网上找到
  • java实现excel价格、收益率函数[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 将表行从 Word 文档复制到现有文档表特定单元格

    我正在寻找一个宏 它将内容从一个 Word 文档中的表格复制到另一个现有 Word 文档中的表格到特定单元格中 从第 5 行开始 复制后面的所有行并将其粘贴到现有文档中的第 5 行 这可能吗 在此输入图像描述 https i stack i
  • 如何在未安装 Office 的情况下以编程方式创建、读取、写入 Excel?

    我对所有读取 写入 创建 Excel 文件的方法感到非常困惑 VSTO OLEDB 等 但它们都seem具有必须安装office的要求 这是我的情况 我需要开发一个应用程序 它将以 Excel 文件作为输入 进行一些计算并创建一个新的 Ex
  • 如何禁用 openpyxl 表中的自动过滤器?

    当我使用 openpyxl 创建表时 它默认在所有列上添加自动过滤器 使用中提供的示例可以重现该行为文档 https openpyxl readthedocs io en stable worksheet tables html 我想显示没
  • OpenArgs 为空问题

    我正在使用OpenArgs使用时发送值的参数DoCmd OpenForm DoCmd OpenForm frmSetOther acNormal acFormAdd acDialog value 然后我用Me OpenArgs在打开的表格内
  • 在 VBA 中循环合并单元格

    是否可以循环遍历合并的单元格vba questions tagged vba 我的范围内有 6 个合并单元格B4 B40 我只需要这 6 个单元格中的值 6 次迭代 上面的答案看起来已经让你排序了 如果您不知道合并的单元格在哪里 那么您可以
  • 如何在字符串vba中包含引号

    我想存储以下文本 Test1 Monday Test Abcdef 全部在字符串中包含引号 我知道要在字符串中包含引号 我必须包含 之前 但在这里这不是一个很好的解决方案 因为我在文本中有太多这样的解决方案 知道如何一次完成这一切吗 您有两
  • Confluence:使用 VBA 更新现有页面

    我尝试使用 VBA 更新 Confluence 页面 我的想法是使用REST API加载页面内容 修改内容然后上传修改后的版本 这是我的代码 Private Sub TestRESTApi Dim uname As String uname

随机推荐