【问题标题】:Compare two columns highlight cells and display address of the cell比较两列高亮单元格并显示单元格地址
【发布时间】:2021-08-25 07:36:40
【问题描述】:

我需要将两列与数字进行比较。可能有多个重复项。我需要开始通过 B 在 C1 中查找第一个值,并突出显示与红色的第一个重合并将该单元格的地址放入 D1 现在我只突出显示第一个巧合代码

Sub Find_First()
Dim FindString As String
Dim myColor As Variant
Dim Rng As Range
myColor = Array("3")
On Error GoTo 0
FindString = Worksheets("2017").Range("C1").Value
    With TargetRange
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Rng.Interior.ColorIndex <> 3 Then
        If Not Rng Is Nothing Then
            Rng.Interior.ColorIndex = myColor(I)
        Else
            MsgBox "Nothing found"
        End If
        Else
        MsgBox "Colored"
        End If
    End With
End Sub

【问题讨论】:

    标签: excel vba find compare highlight


    【解决方案1】:
    hy
    
    Sub Find()
    
        Dim WhatColumn As String, WhereColumn As String, ResultColumn As String
        
        WhatColumn = "A" 'main
        WhereColumn = "B" 'duplicates
        ResultColumn = "C" 'address of main
        
        'for speed
        Dim screenUpdate As Boolean, calc As Variant
        screenUpdate = Application.ScreenUpdating
        calc = Application.Calculation
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        With ActiveSheet
            
            Dim i As Long, q As Long
            'trough te main
            For i = 1 To .Cells(.Rows.Count, WhatColumn).End(xlUp).Row
                'sub loop 4 duplicates
                For q = 1 To .Cells(.Rows.Count, WhereColumn).End(xlUp).Row
                    If .Range(WhatColumn & i).Value = .Range(WhereColumn & q).Value Then
                        'we find ig
                        .Range(ResultColumn & q).Value = WhatColumn & i
                    End If
                Next q
            Next i
    
        End With
        
        'restore default
        Application.ScreenUpdating = screenUpdate
        Application.Calculation = calc
    
    
    End Sub
    

    【讨论】:

    • 酷!谢谢!但我需要找到独特的价值。这就是为什么我想强调首次建立的价值并在下一个查找过程中忽略。
    【解决方案2】:

    我会用for

    sub compare()
    
    LastRow = Cells(Rows.Count, "C").End(xlUp).Row
    
    for i = 1 to LastRow
       if cells(i,3).value=cells(i,2).value then
            cells(i,2).interior.colorindex=3
            cells(i,3).interior.colorindex=3
            cells(i,4).value="B" & i
       end if
    next i
    
    end sub
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多