选择要突出显示的单元格时出现错误

问题描述 投票:0回答:1

我正在尝试编写一个代码,允许您单击带有文本的单元格并浏览列并突出显示具有相同文本的单元格,但我收到错误。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    With Sheets("Sheet1")
    
        .Cells.Interior.ColorIndex = xlColorIndexNone
        
        Select Case Target.Address
        
            Case "$N$6"
                '.Range("B3:L3").Interior.Color = RGB(255, 255, 0)
                row_num = 2
                Count = 0
                Do
                DoEvents
                    row_num = row_num + 1
                    text_review = Sheets("Sheet1").Range("I2:I8" & row_num)
                        If InStr(text_review, text_review) > 0 Then
                            text_review.Interior.Color = RGB(255, 255, 0)
                            Count = Count + 1
                        
                        End If
                    Loop Until text_review = ""
                
        End Select
    End With
End Sub

(https://i.sstatic.net/KPsEUNcG.png)](https://i.sstatic.net/3K1LwkYl.png)

如果有人可以看一下并让我知道我做错了什么,那就太好了,谢谢:)

我刚刚调整了一些角色

excel vba
1个回答
0
投票

尝试这样的事情:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim v, c As Range, rng As Range
    
    If Target.Address <> "$N$6" Then Exit Sub
    
    v = Trim(Target.Value) 'the value of the seelcted cell
    
    'cells to check for `v`
    Set rng = Me.Range("I2", Me.Cells(Rows.count, "I").End(xlUp))
    rng.Interior.ColorIndex = xlNone 'clear previous
    If Len(v) = 0 Then Exit Sub 'nothing to look for
    
    For Each c In rng.Cells
        If InStr(1, c.Value, v, vbTextCompare) > 0 Then
            c.Interior.Color = RGB(255, 255, 0)
        End If
    Next c
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.