Help with colour in code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I have the code below which looks on sheet 1 and puts on sheet 2 the number in the adjacent column. What do I add to the code that once the data is copied into the cell in sheet 2 it colours the cell yellow so I can idenitfy it? Thanks

Code:
Sub DazzawmsCode()
Dim LR As Long, i As Long, Found As Range
With Sheets("Sheet2")
LR = .Range("AB" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("AB" & i)
Set Found = Sheets("Sheet1").UsedRange.Find(what:=.Value, LookIn:=xlValues, Lookat:=xlWhole)
If Not Found Is Nothing Then .Offset(, 1).Value = Found.Offset(, 1).Value
End With
Next i
End With
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try

Code:
If Not Found Is Nothing Then
    .Offset(, 1).Value = Found.Offset(, 1).Value
    .Offset(, 1).Interior.ColorIndex = 6
End If
 
Upvote 0
Try

Code:
If Not Found Is Nothing Then
    .Offset(, 1).Value = Found.Offset(, 1).Value
    .Offset(, 1).Interior.ColorIndex = 6
End If

That seemed to turn the entire column yellow!?
 
Upvote 0
It shouldn't do - it should only copy values and colour the cell when a match is found.
 
Upvote 0
It shouldn't do - it should only copy values and colour the cell when a match is found.

Well it did!! I have posted the code as it is now so you can see if there isnt anything untoward.

Code:
Sub DazzawmsCode()
Dim LR As Long, i As Long, Found As Range
With Sheets("Sheet2")
LR = .Range("AB" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("AB" & i)
Set Found = Sheets("Sheet1").UsedRange.Find(what:=.Value, LookIn:=xlValues, Lookat:=xlWhole)
If Not Found Is Nothing Then .Offset(, 1).Value = Found.Offset(, 1).Value
        .Offset(, 1).Interior.ColorIndex = 6                           
End With
Next i
End With
End Sub
 
Upvote 0
It should be (as per my original reply)

Code:
Sub DazzawmsCode()
Dim LR As Long, i As Long, Found As Range
With Sheets("Sheet2")
    LR = .Range("AB" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        With .Range("AB" & i)
            Set Found = Sheets("Sheet1").UsedRange.Find(what:=.Value, LookIn:=xlValues, Lookat:=xlWhole)
            If Not Found Is Nothing Then
                .Offset(, 1).Value = Found.Offset(, 1).Value
                .Offset(, 1).Interior.ColorIndex = 6
            End If
    End With
    Next i
End With
End Sub
 
Upvote 0
That doesnt work at all comes up with an error End If without block if.
 
Upvote 0
It works now, the only difference I can see between your code and mine is I had the 2 offsets on different lines. I didnt think that would make a difference. Also why is there an End If in the code now after those 2 offsets?
 
Upvote 0
There is an End If because it is now a block If instead of your original in-line If.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,853
Members
452,948
Latest member
UsmanAli786

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
Back
Top