Question : Excel Macro to Compare Cells and Edit the Workbook

I am hoping that someone will be able to create an Excel macro for me to help automate a process that otherwise takes me a long time to complete.

 In my Excel file, I have Worksheet 1 that contains a large number of columns.  In Column E, titled SKU, I have a list of SKU numbers.  In Worksheet 2, in Column A, titled SKU, I have a list of SKU numbers.  

I want to find the matching SKUs between Worksheet 1 and Worksheet 2.   If SKUs that match between Worksheet 1 and Worksheet 2, I want to change the following columns in Worksheet 1 for that particular row:

- Column BF should have the value Disabled
- Column BM should have the value DISCONTINUED
- Column BP should have the value 0
- Column BZ should have the value 0

On worksheet 2, if a match was found then I want Column B to have the value Updated.  If there was not a match found then I want the value to be Not Updated for that particular row.

Thanks for the help!

Answer : Excel Macro to Compare Cells and Edit the Workbook

If I understood your direction correctly, then this will do what you asked for.  Place this code in any module.  See the attached which contains a working example.  Have a nice day!  :)


Option Explicit
'assumes headers are in row one
Sub YourMacro()
   Dim MatchThis As Range, FoundThis As Range, DidNotFindThis As Range
   Dim LastRowDataSheet1 As Long, LastRowDataSheet2 As Long
   Dim Sheet1SearchRange As Range, Sheet2SearchRange As Range
   
   On Error GoTo Err_YourMacro
   
   Application.EnableEvents = False
   Application.ScreenUpdating = False
   
   LastRowDataSheet1 = Sheets("Sheet1").Columns(5).Cells(Rows.Count).End(xlUp).Row
   LastRowDataSheet2 = Sheets("Sheet2").Columns(1).Cells(Rows.Count).End(xlUp).Row
   Set Sheet1SearchRange = Sheets("Sheet1").Range("E2:E" & LastRowDataSheet1)
   Set Sheet2SearchRange = Sheets("Sheet2").Range("A2:A" & LastRowDataSheet2)
   
   For Each MatchThis In Sheet1SearchRange
       With Sheet2SearchRange
           .AutoFilter 1, MatchThis
           For Each FoundThis In .Parent.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
               If FoundThis = MatchThis Then
                   FoundThis.Offset(, 1) = "Updated"
                   With MatchThis.EntireRow
                       .Cells(58) = "Disabled"
                       .Cells(65) = "DISCONTINUED"
                       .Cells(68) = 0
                       .Cells(78) = 0
                   End With
               End If
           Next
       End With
   Next MatchThis
   
   Sheet2SearchRange.AutoFilter
   Set Sheet2SearchRange = Sheet2SearchRange.Offset(, 1)
   For Each DidNotFindThis In Sheet2SearchRange
       If DidNotFindThis <> "Updated" Then DidNotFindThis = "Not Updated"
   Next
    Application.ScreenUpdating = True
   Application.EnableEvents = True
    Exit Sub
   
Err_YourMacro:
   Application.ScreenUpdating = True
   Application.EnableEvents = True
   MsgBox Err.Description
End Sub

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
Option Explicit

'assumes headers are in row one
Sub YourMacro()
    Dim MatchThis As Range, FoundThis As Range, DidNotFindThis As Range
    Dim LastRowDataSheet1 As Long, LastRowDataSheet2 As Long
    Dim Sheet1SearchRange As Range, Sheet2SearchRange As Range
    
    On Error GoTo Err_YourMacro
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    LastRowDataSheet1 = Sheets("Sheet1").Columns(5).Cells(Rows.Count).End(xlUp).Row
    LastRowDataSheet2 = Sheets("Sheet2").Columns(1).Cells(Rows.Count).End(xlUp).Row
    Set Sheet1SearchRange = Sheets("Sheet1").Range("E2:E" & LastRowDataSheet1)
    Set Sheet2SearchRange = Sheets("Sheet2").Range("A2:A" & LastRowDataSheet2)
    
    For Each MatchThis In Sheet1SearchRange
        With Sheet2SearchRange
            .AutoFilter 1, MatchThis
            For Each FoundThis In .Parent.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
                If FoundThis = MatchThis Then
                    FoundThis.Offset(, 1) = "Updated"
                    With MatchThis.EntireRow
                        .Cells(58) = "Disabled"
                        .Cells(65) = "DISCONTINUED"
                        .Cells(68) = 0
                        .Cells(78) = 0
                    End With
                End If
            Next
        End With
    Next MatchThis
    
    Sheet2SearchRange.AutoFilter
    Set Sheet2SearchRange = Sheet2SearchRange.Offset(, 1)
    For Each DidNotFindThis In Sheet2SearchRange
        If DidNotFindThis <> "Updated" Then DidNotFindThis = "Not Updated"
    Next

    Application.ScreenUpdating = True
    Application.EnableEvents = True

    Exit Sub
    
Err_YourMacro:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    MsgBox Err.Description
End Sub
 
Working example workbook
 
Random Solutions  
 
programming4us programming4us