Well, here is my answer, hoping to give some help.
If you want to use VBA:
Explanations, in the comments.
Sub importaData()
Dim r 'to record the last rows in column b in sheet1
Dim B 'column number in sheet1
Dim A 'column number in sheet2
Dim sht1 As Worksheet 'to store the sheet1
Dim sht2 As Worksheet 'to store the sheet2
Dim i
Dim n 'index
Dim rngDsht2 As Range 'D column in sheet2
Dim rngBsht1 As Range 'B column in sheet1
Dim rngAsht2 As Range 'A column in sheet2
Dim tmpB 'one cell of column B (of sheet1)
Dim tmpD 'one cell of column D (OF SHEET2)!
Dim tmpA 'one cell of column a (of sheet1)
r = Range("B1").End(xlDown).Row 'As said, found the last row in column B
B = 2 'Just the number of the columns
A = 1 'Just the number of the columns
Set sht1 = Sheets("Sheet1") 'Storing sheets objects into vars
Set sht2 = Sheets("Sheet2")
Set rngBsht1 = sht1.Range(Cells(1, 2), Cells(r, B)) 'the range of columns B with the data to compare
'Set rngDsht1 = Range("B1" & Cells(r, B)) 'You can use this instead.
sht2.Activate 'Go to sheet2 to set some ranges
Set rngAsht2 = sht2.Range(Cells(1, 1), Cells(r, 1)) 'set the range of column A. "r" is the range of columns B in sheet1
Set rngDsht2 = sht2.Range(Cells(1, 4), Cells(r, 4)) 'set the range of column D. "r" is the range of columns B in sheet1
'Set rngDsht2 = sht2.Range("B1" & Cells(r, B)) 'You can use this instead.
sht1.Activate 'Go back to sheet1
n = 0 'Ini the var
For Each i In rngBsht1 'For each cell into a range of column B
n = n + 1 'increase the var by one every iteration
tmpB = i.Value 'store the value of the cell
tmpA = rngAsht2(i.Row, 1) 'store the value of the cell
tmpD = rngDsht2(i.Row, 1) 'store the value of the cell
If tmpB = tmpA Then 'make the comparison
i.Offset(0, 2).Value = tmpD 'if equal, put the value of column D (sheet2) into cells of columns D in sheet 1
Else
i.Offset(0, 2).Value = Date - 1 'if not equal, put in column D sheet 1 the value of yesterday (the date)
End If
Next i
End Sub
If you want ot use formulas:
In D column in sheet1 you need to put this formula:
=IF(B1=Sheet2!A1,Sheet2!D1,TODAY()-1)
Summary
In sheet1:
Columns B: with data
Columns D: with no data, we need to put data into D column
In sheet2:
Columns A: With data, used to compare with data in column B in sheet1
Columns D: With data, to take, in case any cell in sheet1.ColumnB and sheet2.columnA were equal, and put into columnD in sheet1...
Sheets(1)to refer to the second sheet in order useSheets(2). TheSheets()can either take the index number which is the order of sheets, or it can be a string which is the sheet name. - Scott CranerIIf()only available in VBA? But OP, you're looking to do an If statement, by checking the sheet's index,Sheets("Sheet1").Index- BruceWayneIF()(one I). Change my last comment to It's a simple IF() function call. - Ken White