VBA to compare column A in sheet 1 & 2, enter value if matched and highlight values that do not.

bdbly

New Member
Joined
Jan 8, 2018
Messages
11
Hello All,

I have 2 worksheets named Year 1 and Year 2 with categories. What would be the VBA to compare column A (categories) in sheet Year 1 & Year 2, enter value of column B/Year 1 in column B/Year 2 if matched, and highlight Year 2/Column A categories that are not in Year 1?

Thanks!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Here is what I'm trying to accomplish:

ABAB
1
Sheet 2 (Year 2)

<tbody>
</tbody>
1Sheet 1 (Year 1)
22
3
Description

<tbody>
</tbody>
Code3
Description

<tbody>
</tbody>
Code

<tbody>
</tbody>
4A
red

<tbody>
</tbody>
4Ared
5B
blue

<tbody>
</tbody>
5Bblue
6F
green

<tbody>
</tbody>
6Fgreen
7C7Gyellow
8G
yellow

<tbody>
</tbody>
8Worange
9W
orange

<tbody>
</tbody>
9Yblack
10X10
11Y
black

<tbody>
</tbody>
11

<tbody>
</tbody>
 
Upvote 0
Try this
Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set sh1 = Sheets("Year 1")
Set sh2 = Sheets("Year 2")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh1.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                c.Offset(, 1) = fn.Offset(, 1).Value
            Else
                c.Interior.Color = vbYellow
            End If
    Next
End Sub

It only uses one color.
 
Upvote 0
Would it be possible to compare multiple columns in the 2 worksheets, copy code from year 1 to year 2 when there is a match and highlight column values that are new in Year 2? Thanks again!

Excel 2016 (Windows) 32 bit
ABCDEFGHI
5Year 2Code
6a
7b
8c0.00
9d0.00
10e0.00
11f 0.00
12g 0.00
13h0.00
14i0.00
15j0.00
16k0.00
17l 0.00
18m 0.00
19n0.00
20o 0.00
21p0.00

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Year 2



Excel 2016 (Windows) 32 bit
ABCDEFGHI
5Year 1Code
6a
7b
8c0.00red
9d0.00blue
10x0.00black
11y0.00purple
12z0.00brown
130.00
140.00
15j0.00yellow
160.00
17l 0.00pink
18m 0.00other
19n0.00silver
20o 0.00gold
21p0.00green

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Year 1
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,576
Members
449,173
Latest member
Kon123

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