Code problem

Jim28

Board Regular
Joined
Apr 2, 2006
Messages
165
Hi all

I have following code but I have a problem with the following line:

Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next

It highlights all the cells above red rather the one line it is supposed to.

Any ideas?

Sub HighlightTheDifferences()
Dim LstRw1 As Long, LstCol1 As Long, List1 As Range
Dim LstRw2 As Long, LstCol2 As Long, List2 As Range
Dim FirstSht As String, SecondSht As String
Dim SrchName As Range, FndName As Range, RedRng As Range
Dim LstCol As Long, i As Integer
FirstSht = Sheets(1).Name
SecondSht = Sheets(2).Name
Application.ScreenUpdating = False

With Sheets(FirstSht)
.Cells.Interior.ColorIndex = xlNone
LstRw1 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol1 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw1)
.Range("A1:A" & LstRw1) = .Range("A1:A" & LstRw1).Value
End With
Set List1 = .Range("A1:A" & LstRw1)
End With

With Sheets(SecondSht)
.Cells.Interior.ColorIndex = xlNone
LstRw2 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol2 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw2)
.Range("A1:A" & LstRw2) = .Range("A1:A" & LstRw2).Value
End With
Set List2 = .Range("A1:A" & LstRw2)
End With

If LstCol1 > LstCol2 Then LstCol = LstCol1 + 1 Else LstCol = LstCol2 + 1

For Each SrchName In List1
If Application.WorksheetFunction.CountA(List2, SrchName) > 0 Then
Set FndName = List2.Find(SrchName, LookAt:=xlContents)
If Not FndName Is Nothing Then
For i = 1 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> Sheets(SecondSht).Cells(FndName.Row, i).Value Then
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 6
Sheets(SecondSht).Cells(FndName.Row, i).Interior.ColorIndex = 6
End If
Next i
Else
For i = 2 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> "" Then _
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 4
Next i
End If
End If
Next SrchName

For Each SrchName In List2
With Sheets(FirstSht)
Set FndName = List1.Find(SrchName, LookAt:=xlContents)
If FndName Is Nothing Then
SrchName.EntireRow.Copy .Cells(Rows.Count, "a").End(xlUp)(6)
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = xlNone
On Error GoTo 0
End If
End With
Next SrchName

Sheets(FirstSht).Columns(1).EntireColumn.Delete
Sheets(SecondSht).Columns(1).EntireColumn.Delete
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Perhaps you need the . at the very beginning

change
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3

to
.Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Hi all

I have following code but I have a problem with the following line:

Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next

It highlights all the cells above red rather the one line it is supposed to.

Any ideas?
try
Code:
.Cells(Rows.Count, "A").End(xlUp).Resize(,LstCol).Interior.ColorIndex = 3
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,550
Messages
5,832,423
Members
430,132
Latest member
Bdesai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top