问题描述
我需要用数字比较两列。可能有多个副本。我需要开始在 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