Excel - Compare two lists and change row color
On April 21,2022 by Tom RoutleyIssue
I have two different workbooks; let's say workbook 1 and workbook2. In both workbooks I have a column with barcodes.
I need a VBA to check the two barcode columns in workbooks and if it find matches to create a new workbook and copy the whole row of workbook 1 and use the same color of cell barcode in workbook 2.
Solution
I am assuming that the barcode columns both sheet 1 and sheet 2 are the same
then try this code
at the beginning when you run the code, the code will ask an input about the column letter of the barcode. you can type the column letter e.g. G and click ok (on the top right):
The macro is
Sub test() Dim col As String, r As Range, c As Range, cfind As Range Dim x, y As Integer col = InputBox("type the column LETTER in which the barcode is netered for e.g. G") On Error Resume Next With Worksheets("sheet2") Set r = Range(.Cells(2, col), .Cells(2, col).End(xlDown)) For Each c In r x = c.Value With Worksheets("sheet1").Columns(col & ":" & col) Set cfind = .Cells.Find(what:=x, lookat:=xlWhole) If cfind Is Nothing Then GoTo nnext y = cfind.Interior.ColorIndex cfind.EntireRow.Copy With Worksheets("sheet3") .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial .Cells(Rows.Count, col).End(xlUp).Interior.ColorIndex = y End With End With nnext: Next c End With End Sub
Note
Thanks to venkat1926 for this tip on the forum.
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply