Merge values from old sheet with new sheet and flag new entries

covareo

New Member
Joined
Apr 28, 2006
Messages
27
Office Version
  1. 365
Platform
  1. MacOS
I have a task that is asked of my team every year that is very time-consuming. We get a listing of all employees that is missing key information that we manually update each year. I am hoping that you can help us to automate this process. We have a new list as the "Working" tab in an Excel file and last year's list as the "Old" tab in the same file (attached). I am hoping to have a formula or script that can search each Employee_Num from the "Working" tab for information on the "Old" tab, and if it's found, populate columns C, D, and E with any data found. Additionally, if the record is not found in "Old", mark column A on the "Working" sheet with an X so we can examine and confirm its a new hire. I have mocked this up in the "Result" tab.

The lookup value is the Employee_Num field. The first column is an automatically generated iterative counter and not important. If values exist in the "Working" tab they should be respected and kept as they are. Empty values are common and inconsistent.

Here is the "Working" Tab:
Employee-Update.xlsx
ABCDEFGHIJ
1NewHire_DateTypeTerm_DateEmployee_NumLastNameFirstNameInitialDepend
21 001234567La RosaLe ShawnL0
32 003456789WilsonBarbaraW4
43 055527240D'AngeloGarettS2
54 011846457McDoughBetty 0
65 016234437RinniJoshuaM0
762/1/21PT7/14/23 001423289OlsonEnrique 2
87 001293213EricksonKyle 0
Working


Here is the "Old" tab:
Employee-Update.xlsx
ABCDEFGHI
1Hire_DateTypeTerm_DateEmployee_NumLastNameFirstNameInitialDepend
216/14/12FT 001234567La RosaLe ShawnL0
322/24/92FT 003456789WilsonBarbaraW4
439/15/23FT 055527240D'AngeloGarettS2
54FT 011846457McDoughBetty 0
653/13/21FT 016234437RinniJoshuaM0
762/1/21PT7/14/23 001423289OlsonEnrique 2
Old



Here is a mocked-up "Result" after the run:
Employee-Update.xlsx
ABCDEFGHIJ
1NewHire_DateTypeTerm_DateEmployee_NumLastNameFirstNameInitialDepend
216/14/12FT 001234567La RosaLe ShawnL0
322/24/92FT 003456789WilsonBarbaraW4
439/15/23FT 055527240D'AngeloGarettS2
54FT 011846457McDoughBetty 0
653/13/21FT 016234437RinniJoshuaM0
762/1/21PT7/14/23 001423289OlsonEnrique 2
87X 001293213EricksonKyle 0
Result
 
I thought the requirement was to put an X in column B if they are are new employee.

If you don't want the X in column B you can remove this section of the code:

This will copy hire_date, type, term_data from Old to New
VBA Code:
Public Sub EmployeeTransfer()
    
    Dim wWs As Worksheet, oWs As Worksheet
    Dim ar As Variant
    Dim rng As Range
    Dim i As Long, x As Long
    Dim empNum As Variant
    
    Set wWs = ThisWorkbook.Sheets("Working")
    Set oWs = ThisWorkbook.Sheets("Old")

    ar = oWs.Range("A1").CurrentRegion
    Set rng = wWs.Range("A1").CurrentRegion
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For i = 2 To rng.Rows.Count   ' leave the header out
       empNum = rng(i, 6).Value
    
        ' loop to find out if in OLD
        For x = 1 To UBound(ar)
            If ar(x, 5) = empNum Then
                wWs.Cells(i, 3).Value = ar(x, 2)
                wWs.Cells(i, 4).Value = ar(x, 3)
                wWs.Cells(i, 5).Value = ar(x, 4)
            End If
        Next
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
     
    Set oWs = Nothing
    Set wWs = Nothing
End Sub
 
Upvote 1

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I tried doing this with VLOOKUP, but it was always blank/FALSE like this VBA has returned - is it because they are dates being copied? I tried:
Excel Formula:
=IFERROR(VLOOKUP(E2, 'Old'!B:E, 2, FALSE), "")
Actually using this formula seems to work when i re-order columns so Employee_Num is before the other columns:

Excel Formula:
=IFERROR(VLOOKUP(B2, 'Old'!B:E, 1, FALSE), "")

Question: How do I have the cells that DON'T have results stay BLANK/NULL as opposed to 1/0/00 for Dates and 0 for Type?
 
Upvote 0
I thought the requirement was to put an X in column B if they are are new employee.

If you don't want the X in column B you can remove this section of the code:

This will copy hire_date, type, term_data from Old to New
VBA Code:
Public Sub EmployeeTransfer()
   
    Dim wWs As Worksheet, oWs As Worksheet
    Dim ar As Variant
    Dim rng As Range
    Dim i As Long, x As Long
    Dim empNum As Variant
   
    Set wWs = ThisWorkbook.Sheets("Working")
    Set oWs = ThisWorkbook.Sheets("Old")

    ar = oWs.Range("A1").CurrentRegion
    Set rng = wWs.Range("A1").CurrentRegion
   
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    For i = 2 To rng.Rows.Count   ' leave the header out
       empNum = rng(i, 6).Value
   
        ' loop to find out if in OLD
        For x = 1 To UBound(ar)
            If ar(x, 5) = empNum Then
                wWs.Cells(i, 3).Value = ar(x, 2)
                wWs.Cells(i, 4).Value = ar(x, 3)
                wWs.Cells(i, 5).Value = ar(x, 4)
            End If
        Next
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    Set oWs = Nothing
    Set wWs = Nothing
End Sub

This does not seem to return anything - here is the sheet after running:
Employee-Update.xlsx
ABCDEFGHIJ
1NewHire_DateTypeTerm_DateEmployee_NumLastNameFirstNameInitialDepend
21 001234567La RosaLe ShawnL0
32 003456789WilsonBarbaraW4
43 055527240D'AngeloGarettS2
54 011846457McDoughBetty 0
65 016234437RinniJoshuaM0
762/1/21PT7/14/23 001423289OlsonEnrique 2
87 001293213EricksonKyle 0
Working


I think I'm super close w VLOOKUP but just need to preserve the blanks instead of replacing blank dates with "1/0/00" and blank Types with "0". To get this result I had to re-order my columns to have what was F (Employee_Num) move to Column B before the other values to be copied (I did this in a separate sheet so as not to affect the code you provided). You can see the results of my VLOOKUP here:

Employee-Update-Vlookup.xlsx
ABCDEFGHIJ
1NewEmployee_NumHire_DateTypeTerm_DateLastNameFirstNameInitialDepend
21 0012345676/14/12FT1/0/00La RosaLe ShawnL0
32 0034567892/24/92FT1/0/00WilsonBarbaraW4
43 0555272409/15/23FT1/0/00D'AngeloGarettS2
54 0118464571/0/0001/0/00McDoughBetty 0
65 0162344373/13/21FT1/0/00RinniJoshuaM0
76 0014232892/1/21PT7/14/23OlsonEnrique 2
87 001293213   EricksonKyle 0
Working
Cell Formulas
RangeFormula
D2:D8D2=IFERROR(VLOOKUP(C2, Old!B:E, 2, FALSE), "")
E2:E8E2=IFERROR(VLOOKUP(C2, Old!B:E, 3, FALSE), "")
F2:F8F2=IFERROR(VLOOKUP(C2, Old!B:E, 4, FALSE), "")




Excel Formula:
=IFERROR(VLOOKUP(B2, 'Old'!B:E, 1, FALSE), "")

Any advice on how to keep the NULL/Blank values in this method?
 
Upvote 0
For anyone who sees this and is struggling with the same issue here is the solution eventually came to:

  1. VLOOKUP only searches the first column of data for matches, so I had to re-order columns to have the search field first
  2. I had to specify date formatting or the VLOOKUP just brought the unformatted time integer
  3. I added some additional functions to preserve blanks as the default would add 0's to blanks.
I ended up using the following:

Excel Formula:
=IF(ISBLANK(VLOOKUP(B2, 'Old'!B:E, 2, FALSE)), "", IFERROR(VLOOKUP(B2, 'Old'!B:E, 2, FALSE), ""))
 
Upvote 0
Solution

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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