To begin, I will declare the variables:
Dim userId As String
Dim found As Range
Dim theDate As String
Next, I am going to loop through all of the cells that I want. In my example, I have 1730 user names to lookup (you can see why I wrote a script). First, it assigns the user name from the first sheet to the "userId" variable. Next, it searches through the "Logins" sheet backwards by matching the user ids (since this sheet is ordered by login date and we want the most recent). If it finds a match, it stores the cell's value in the range "found." If found is empty, it writes in the original cell that the last login date was never. If found is populated (a match was found) then it assigns the date stored in the field to the variable "theDate." Finally, it writes the value of theDate into the original sheet in Cells(i,3) meaning the 3rd column of the i^th row.
For i = 1 To 1730
userId = Cells(i, 1).Value
Set found = Sheets("Logins").Columns(2).Cells.Find(What:=userId, After:=[B1],
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If found Is Nothing Then
Cells(i, 3).Value = "Never"
Else
theDate = Sheets("Logins").Cells(found.Row, 1).Value
Cells(i, 3).Value = theDate
End If
Next i
No comments:
Post a Comment