Need code which analyze the data b/w two sheets and if the same value is found in col A in 2 sheets then it needs to check adjacent cell

Sunil113

New Member
Joined
Sep 18, 2022
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Need code which performs the below operations.
Info :
I have two sheets with data in an excel. The data in both the sheets will be in same format.
Sheet 1 will be having header.
Sheet 2 will be without header.
Operations to be performed:
1. Value in sheet 1 Cell A2 needs to be checked with value in Sheet 2 entire column A.
2. If the value is matching then it needs to check the adjacent cell value (column B) in both the sheets.
3. If the value in sheet 2 is greater than the value in sheet 1 then the entire row (from column B to column Z) needs to be copy paste to sheet 1.

Thank you
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Where in Sheet1 do you want to paste the entire row?
 
Upvote 0
Where in Sheet1 do you want to paste the entire row?
Please find the attached images for better understanding.
In the attached excel, I have data in 2 sheets, Sheet 1 with headers & sheet 2 without headers. In sheet 1 no.of columns is more compared to sheet 2
1. Value in sheet 1 Cell A2 needs to be checked with value in Sheet 2 entire column A. __ Need to check if sachin name was available in sheet 2
2. If the value is matching then it needs to check the adjacent cell value (column B) in both the sheets. ___ If Sachin name is available in Sheet 2 then it needs to compare the performance score with sheet 1
3. If the value in sheet 2 is greater than the value in sheet 1 then the entire row (from column B to column Z) needs to be copy paste to sheet 1. __ if Sachin performance is score is more in sheet 2 then the data in sheet 1 needs to be replaced by data in sheet 2 for sachin row.
 

Attachments

  • excel ex 3.PNG
    excel ex 3.PNG
    25.2 KB · Views: 4
  • excel ex 2.PNG
    excel ex 2.PNG
    16.7 KB · Views: 4
  • excel ex.PNG
    excel ex.PNG
    24.9 KB · Views: 5
Upvote 0
if Sachin performance is score is more in sheet 2 then the data in sheet 1 needs to be replaced by data in sheet 2 for sachin row.
This part I understand. You want to replace the existing data in Sheet1. However, in order to do this, each row of data needs to have a unique identifier. The name in column A can be the unique identifier as long as there are no duplicate names. Can duplicate names exist in column A?
If the value in sheet 2 is greater than the value in sheet 1 then the entire row (from column B to column Z) needs to be copy paste to sheet 1.
This part is still not clear. Where in Sheet1 do you want to paste the data?
Also, it is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your two sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
This part I understand. You want to replace the existing data in Sheet1. However, in order to do this, each row of data needs to have a unique identifier. The name in column A can be the unique identifier as long as there are no duplicate names. Can duplicate names exist in column A?

This part is still not clear. Where in Sheet1 do you want to paste the data?
Also, it is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your two sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
Thank you for your time.
yes, each row has a unique Identifier from the attached mini sheet we can consider either name or ID.
No, we don't have a duplicate name in Column A.

Background Story:
Every individual/team has a different statement of work. By end of the day/month/year, they will update their work progress in their respective trackers.
Now, I have a macro to combine all of their tracker's data in a single sheet (Sheet 2) and then copy it to Main Tracker (Sheet 1).
As master tracker is used to check all the work it will be having more columns when compared to individual trackers.
In individual trackers employees might update the remarks/dates/any concerns.
When I run the macro again all the data from their tracker's will be copied to Sheet 2 and again the data will be copied to Master Tracker (Sheet 1).
By following the above process, I will be having Duplicated data in Sheet 1(Sachin with performance score 99 already in sheet 1 and again sachin with performance score 100 data copied from sheet 2) and if there is same data in sheet 2 after copying to sheet 1, I am using remove duplicates with col A & col B which is taking care of duplicates with same name & performance score in both the sheets.

So, I need a way where data in sheet 1 Column A it needs to check the data in sheet 2 column A, if it matches then it needs to check the right adjacent cell and if the value is more than the value in sheet 1 then that particular row needs to be copied to sheet 1 replacing the old data.
At last, the main tracker (Sheet 1) needs to show as Sachin with performance score 100 and his remarks.

Hope you are clear.

Below is the sample data.

Book1.xlsx
ABCDEFGHIJKLMNOPQ
1Employee IDEmployeePerformance Score Work DueEst CompComp TaskCampus BuildingRemarks PriorityBudget AllocatedBudget Approved byBudget UtilisedBalHead CountStatus
21Dhoni3309/23/202309/23/2023Developer7Gavaskar11Work Started
32Ishant3309/23/202309/23/2023Helper9Gavaskar6Work Started
43Kholi3309/23/202309/23/2023IT6Gavaskar15Work Started
54Kumar1009/23/202309/23/2023data Engineer2Gavaskar28Assignment of Work
65Rahul3310/23/202210/23/2022Security5Gavaskar43Work Started
76Rohit3309/23/202309/23/2023Helper8Gavaskar25Work Started
87Sachin9909/25/202209/25/2022Admin4Gavaskar39Task Completed with errors
98Sai6709/23/202309/23/2023Installation3Gavaskar4Phase -2 Completed
109Sunil3309/23/202309/23/2023Statician1Gavaskar18Work Started
Sheet1
Cell Formulas
RangeFormula
Q2:Q10Q2=VLOOKUP(C2,Sheet3!E$3:F$14,2,0)


Book1.xlsx
ABCDEFGHIJ
19Sunil3309/23/202309/23/2023Statician1
24Kumar1009/23/202309/23/2023data Engineer2
38Sai6709/23/202309/23/2023Installation3
47Sachin10009/25/202209/25/202209/23/2022Admin4Task Completed - Assigned seats to all
55Rahul7510/23/202210/23/2022Security5Security patch updated with errors - Clearing errors
63Kholi3309/23/202309/23/2023IT6
71Dhoni5009/23/202309/23/2023Developer7WIP
86Rohit3309/23/202309/23/2023Helper8
92Ishant3309/23/202309/23/2023Helper9
Sheet2


Book1.xlsx
EF
30Need to Start
45Head Count Required
510Assignment of Work
633Work Started
750Phase -1 Completed
858Phase -1 Testing Completed
967Phase -2 Completed
1070Phase -2 Testing Completed
1175Phase -3 Completed
1280Phase -3 Testing Completed
1399Task Completed with errors
14100Completed
Sheet3


Book1.xlsx
ABCDEFGHIJKLMNOPQ
1Employee IDEmployeePerformance Score Work DueEst CompComp TaskCampus BuildingRemarks PriorityBudget AllocatedBudget Approved byBudget UtilisedBalHead CountStatus
21Dhoni3309/23/202309/23/2023Developer7Gavaskar11Work Started
32Ishant3309/23/202309/23/2023Helper9Gavaskar6Work Started
43Kholi3309/23/202309/23/2023IT6Gavaskar15Work Started
54Kumar1009/23/202309/23/2023data Engineer2Gavaskar28Assignment of Work
65Rahul3310/23/202210/23/2022Security5Gavaskar43Work Started
76Rohit3309/23/202309/23/2023Helper8Gavaskar25Work Started
87Sachin9909/25/202209/25/2022Admin4Gavaskar39Task Completed with errors
98Sai6709/23/202309/23/2023Installation3Gavaskar4Phase -2 Completed
109Sunil3309/23/202309/23/2023Statician1Gavaskar18Work Started
119Sunil3309/23/202309/23/2023Statician1
124Kumar1009/23/202309/23/2023data Engineer2
138Sai6709/23/202309/23/2023Installation3
147Sachin10009/25/202209/25/202209/23/2022Admin4Task Completed - Assigned seats to all
155Rahul7510/23/202210/23/2022Security5Security patch updated with errors - Clearing errors
163Kholi3309/23/202309/23/2023IT6
171Dhoni5009/23/202309/23/2023Developer7WIP
186Rohit3309/23/202309/23/2023Helper8
192Ishant3309/23/202309/23/2023Helper9
Sheet1
Cell Formulas
RangeFormula
Q2:Q10Q2=VLOOKUP(C2,Sheet3!E$3:F$14,2,0)


Book1.xlsx
ABCDEFGHIJKLMNOPQ
1Employee IDEmployeePerformance Score Work DueEst CompComp TaskCampus BuildingRemarks PriorityBudget AllocatedBudget Approved byBudget UtilisedBalHead CountStatus
21Dhoni5009/23/202309/23/2023Developer7WIPGavaskar11Phase -1 Completed
32Ishant3309/23/202309/23/2023Helper9Gavaskar6Work Started
43Kholi3309/23/202309/23/2023IT6Gavaskar15Work Started
54Kumar1009/23/202309/23/2023data Engineer2Gavaskar28Assignment of Work
65Rahul7510/23/202210/23/2022Security5Security patch updated with errors - Clearing errorsGavaskar43Phase -3 Completed
76Rohit3309/23/202309/23/2023Helper8Gavaskar25Work Started
87Sachin10009/25/202209/25/202209/23/2022Admin4Task Completed - Assigned seats to allGavaskar39Completed
98Sai6709/23/202309/23/2023Installation3Gavaskar4Phase -2 Completed
109Sunil3309/23/202309/23/2023Statician1Gavaskar18Work Started
Sheet1
Cell Formulas
RangeFormula
Q2:Q10Q2=VLOOKUP(C2,Sheet3!E$3:F$14,2,0)
 
Upvote 0
that particular row needs to be copied to sheet 1 replacing the old data.
You have more columns in Sheet1 than in Sheet2. If the row from Sheet2 replaces the row in Sheet1, you will lose some of the columns in Sheet1. Please clarify in detail.
 
Upvote 0
You have more columns in Sheet1 than in Sheet2. If the row from Sheet2 replaces the row in Sheet1, you will lose some of the columns in Sheet1. Please clarify in detail.
Is it possible to copy the data in sheet 2 of certain range instead of entire row. (Copy Only Cells with data) to sheet 1
 
Upvote 0
Try:
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim i As Long, srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object
    Set desWS = Sheets("Sheet1")
    Set srcWS = Sheets("Sheet2")
    v1 = desWS.Range("A2", desWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    v2 = srcWS.Range("A1", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v2) To UBound(v2)
        If Not dic.exists(v2(i, 2)) Then
            dic.Add v2(i, 2), v2(i, 3)
        End If
    Next i
    For i = LBound(v1) To UBound(v1)
        If dic.exists(v1(i, 2)) Then
            If dic(v1(i, 2)) > v1(i, 3) Then
                With srcWS
                    .Range("A" & i).Resize(, 9).Copy desWS.Range("A" & i + 1)
                    .Range("J" & i).Copy desWS.Range("Q" & i + 1)
                End With
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thank you So Much
this really helps to solve the issue.
Will let you know if I face any issues.
if possible could you please explain the code in brief.
 
Upvote 0
You are very welcome. :) I hope this helps:
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim i As Long, srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object
    Set desWS = Sheets("Sheet1")
    Set srcWS = Sheets("Sheet2")
    v1 = desWS.Range("A2", desWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value 'assigns values in columns A, B and C of Sheet1 to an array
    v2 = srcWS.Range("A1", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value 'assigns values in columns A, B and C of Sheet2 to an array
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v2) To UBound(v2) 'loops through names in Sheet2
        If Not dic.exists(v2(i, 2)) Then
            dic.Add v2(i, 2), v2(i, 3) 'adds names to the dictionary
        End If
    Next i
    For i = LBound(v1) To UBound(v1) 'loops through names in Sheet1
        If dic.exists(v1(i, 2)) Then 'checks if names exist in the dictionary
            If dic(v1(i, 2)) > v1(i, 3) Then 'compares values in column C of both sheets
                With srcWS 'if value in column C of Sheet2 is greater than value in column C of Sheet1---
                    .Range("A" & i).Resize(, 9).Copy desWS.Range("A" & i + 1) 'copies A:I from Sheet2 to Sheet1
                    .Range("J" & i).Copy desWS.Range("Q" & i + 1) 'copies column J from Sheet2 to column Q of Sheet1
                End With
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,701
Messages
6,126,292
Members
449,308
Latest member
VerifiedBleachersAttendee

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