Match dates from sheet1 to sheet2, then copy data back to sheet1 to a different column

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Please can someone help me?

On sheet1 I have 51 random dates in column B ranging from B7:B58.

On sheet2, I have a long list of dates starting from 01/01/2022 and ending 31/12/2030 (A1:A3287) in column A and in column B is the colour of our shift at work during them dates.

What I would like is a VBA code that can look for each date on sheet1 (B7:B58), and then match them with the date on sheet2. Once it has found the date on sheet2, it then copy’s the shift colour from column B on sheet2 and then pastes it in column C on sheet1 next to the original date.

Any help would be appreciated.

Thanks

Dan
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi
Try either
VBA Code:
Sub test()
    Dim a: Dim i&
    a = Sheets("sheet1").Range("B7:B58")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If a(i, 1) <> 0 Then
                If Not .exists(a(i, 1)) Then .Item(a(i, 1)) = .Item(a(i, 1))
            End If
        Next
        a = Sheets("sheet2").Range("a1:a" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 2)
        For i = 1 To UBound(a)
            If .exists(a(i, 1)) Then .Item(a(i, 1)) = Sheets("Sheet2").Cells(i, 2).Interior.Color
        Next
        For i = 0 To .Count - 1
            Sheets("sheet1").Cells(7 + i, 3).Interior.Color = .Items()(i)
        Next
    End With
End Sub

VBA Code:
Sub test2()
    Dim a: Dim i&
    a = Sheets("sheet1").Range("B7:B58")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If a(i, 1) <> 0 Then
                If Not .exists(a(i, 1)) Then .Item(a(i, 1)) = .Item(a(i, 1))
            End If
        Next
        a = Sheets("sheet2").Range("a1:a" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 2)
        For i = 1 To UBound(a)
            If .exists(a(i, 1)) Then .Item(a(i, 1)) = Sheets("Sheet2").Cells(i, 2)
        Next
        Sheets("sheet1").Cells(7, 3).Resize(.Count) = Application.Transpose(.Items)
    End With
End Sub
Depends the meaning of the shift color in column B
 
Upvote 0
Solution
Hi, mohadin,

Your first code works perfectly, thank you so much.

Thanks

Dan
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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