微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

比较两列高亮单元格和显示单元格地址

如何解决比较两列高亮单元格和显示单元格地址

我需要用数字比较两列。可能有多个副本。我需要开始在 C1 到 B 中查找第一个值并突出显示与红色的第一个重合并将该单元格的地址放入 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

解决方法

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
,

我会用 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。