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

huangapple go评论66阅读模式
英文:

Identify the Words if its matches highlight the value VBA

问题

以下是代码的翻译部分:

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
    
    ' 获取Sheet1中列A的数据的最后一行
    lastRowSheet1 = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Row
    
    ' 获取Sheet2中列A的数据的最后一行
    lastRowSheet2 = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, 1).End(xlUp).Row
    
    ' 循环遍历Sheet1中列A的每一行数据
    For i = 1 To lastRowSheet1
        ' 获取Sheet1当前行列A的值
        cellValueSheet1 = Sheets("Sheet1").Cells(i, 1).Value
    
        ' 将字符串拆分为单词,用于Sheet1
        wordsSheet1 = Split(cellValueSheet1, " ")
    
        ' 循环遍历Sheet2中列A的每一行数据
        For j = 1 To lastRowSheet2
            ' 获取Sheet2当前行列A的值
            cellValueSheet2 = Sheets("Sheet2").Cells(j, 1).Value
    
            ' 将字符串拆分为单词,用于Sheet2
            wordsSheet2 = Split(cellValueSheet2, " ")
    
            ' 循环遍历Sheet1中的每个单词
            For wordIndexSheet1 = 0 To UBound(wordsSheet1)
                ' 循环遍历Sheet2中的每个单词
                For wordIndexSheet2 = 0 To UBound(wordsSheet2)
                    ' 如果单词匹配,将Sheet1中的单词标记为红色高亮显示
                    If StrComp(wordsSheet1(wordIndexSheet1), wordsSheet2(wordIndexSheet2), vbTextCompare) = 0 Then
                        wordSheet1 = wordsSheet1(wordIndexSheet1)
                        ' 在Sheet1中高亮显示单词
                        Sheets("Sheet1").Cells(i, 1).Characters(InStr(cellValueSheet1, wordSheet1), Len(wordSheet1)).Font.ColorIndex = 3 ' 高亮显示为红色
                        Sheets("Sheet1").Cells(i, 2).Value = Sheets("Sheet1").Cells(i, 2).Value & " " & wordSheet1
                    End If
                Next wordIndexSheet2
            Next wordIndexSheet1
        Next j
    Next i
End Sub

请注意,翻译中保留了代码中的变量和注释,以保持代码的完整性。

英文:

Sample data:

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

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

i tried this im not getting 100% accuracy can anyone help?

答案1

得分: 2

以下是使用.Find实现此目标的一种方法。我已经添加了注释,所以您应该可以轻松理解它。如果有疑问,请随时提问。

代码:

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 ws = Sheet1
'这是需要着色的文本范围
Set rng = ws.Range("A1:A6")
'这是包含关键字的范围
InputAr = ws.Range("D1:D5")
'遍历搜索关键字
For i = LBound(InputAr) To UBound(InputAr)
'查找文本
Set aCell = rng.Find(What:=InputAr(i, 1), LookIn:=xlFormulas, LookAt:=xlPart)
'如果找到
If Not aCell Is Nothing Then
Set bCell = aCell
'着色文本
ColorText aCell, InputAr(i, 1)
'查找下一个出现
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
'将找到的范围设置为Nothing
Set aCell = Nothing
End If
Next i
End Sub
'用于着色文本的子程序
Private Sub ColorText(r As Range, keyword As Variant)
Dim sPos As Long
Dim TxtLen As Long
'设置起始位置
sPos = InStr(1, r.Value2, keyword, vbTextCompare)
'获取长度
TxtLen = Len(keyword)
'着色文本
r.Characters(Start:=sPos, Length:=TxtLen).Font.Color = RGB(255, 0, 0)
End Sub

截图:

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

英文:

Here is one way to achieve this using .Find. I have commented the code so you should not have a problem understanding it. If you do then simply ask.

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

Screenshot:

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

答案2

得分: 1

以下是翻译好的内容:

你可以尝试:

Sub Test()
'识别两张表
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets("Sheet2")
'获取每张表的最后一行
Dim lr1 As Long: lr1 = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
Dim lr2 As Long: lr2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
'定义要处理的两个区域
Dim rng1 As Range: Set rng1 = ws1.Range("A1:A" & lr1)
Dim rng2 As Range: Set rng2 = ws2.Range("A1:A" & lr2)
'初始化正则表达式对象
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\b(?:" & Join(Application.Transpose(rng2.Value), "|") & ")\b"        
'循环处理rng1中的每个单元格
For Each cl In rng1
If .Test(cl.Value) Then
For Each m In .Execute(cl.Value)
cl.Characters(m.FirstIndex + 1, m.Length).Font.Color = vbRed
Next
End If
Next
End With
End Sub

这将产生类似下面的结果:

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

注意: 使用正则表达式使得以下操作成为可能:

  • 区分真正的和错误的匹配(例如:'DISORDERLY NUMBERPLATES' <> 'DISORDERLY NUMBER');

  • 在单个单元格中找到相同子字符串的多个匹配项(例如:'TRAFFIC COPS STUCK IN TRAFFIC')。

英文:

You can try:

Sub Test()
&#39;Identify both sheets
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Worksheets(&quot;Sheet1&quot;)
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets(&quot;Sheet2&quot;)
&#39;Retrieve the last used row per sheet
Dim lr1 As Long: lr1 = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
Dim lr2 As Long: lr2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
&#39;Define both ranges to work with
Dim rng1 As Range: Set rng1 = ws1.Range(&quot;A1:A&quot; &amp; lr1)
Dim rng2 As Range: Set rng2 = ws2.Range(&quot;A1:A&quot; &amp; lr2)
&#39;Initialize an regex object
With CreateObject(&quot;vbscript.regexp&quot;)
.Global = True
.Pattern = &quot;\b(?:&quot; &amp; Join(Application.Transpose(rng2.Value), &quot;|&quot;) &amp; &quot;)\b&quot;        
&#39;Loop each cl in rng1
For Each cl In rng1
If .Test(cl.Value) Then
For Each m In .Execute(cl.Value)
cl.Characters(m.FirstIndex + 1, m.Length).Font.Color = vbRed
Next
End If
Next
End With
End Sub

This would result in something like the below:

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

Note: The use of a regular expression made it possible to:

  • Distinguish between true and false positives (e.g.: 'DISORDERLY NUMBERPLATES' <> 'DISORDERLY NUMBER');

  • Find multiple of the same substrings in a single cell (e.g.: 'TRAFFIC COPS STUCK IN TRAFFIC').

huangapple
  • 本文由 发表于 2023年5月10日 19:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定