Compare the values in the first and second columns in all sheets of the workbook with the main one.

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I am looking forward to getting a code that enables me to compare the first and second columns of all the sheets of the workbook with the first and second columns of the main sheet. When the same value is checked, the values of column C, D, and E are copied to the same row. The purpose of the code is to modify the data from the main and copy it back to its place on the sheets. Thank you in advance


VBA Code:
Sub test()

Set sht = sheet1
Set sht2 = sheet2
SrcLastRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
DestLastRow = sht2.Cells(sht2.Rows.Count, 1).End(xlUp).Row
For I = 2 To SrcLastRow
    For k = 2 To DestLastRow
        If sht2.Cells(k, 1).Value = sht.Cells(I, 1).Value And _
           sht2.Cells(k, 2).Value = sht.Cells(I, 2).Value Then _
          
      sht2.Cells(k, 2).Value = sht.Cells(I, 2).Value
      sht2.Cells(k, 3).Value = sht.Cells(I, 3).Value
      sht2.Cells(k, 4).Value = sht.Cells(I, 4).Value
      sht2.Cells(k, 4).Value = sht.Cells(I, 4).Value
      sht2.Cells(k, 5).Value = sht.Cells(I, 5).Value
 End If
Next
Next
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I think that these things are often clearer when we can visually see an actual example.
Can you post a sample of your data, along with your expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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