Excel VBA: Load the Latest Attendance Row into UserForm Text Boxes
Question details
Load a client's most recent recorded attendance row into Excel UserForm text boxes using the client ID in column D and the attendance date in column K.

- Product
- Microsoft Excel VBA and UserForms
- Device & OS
- not provided
- Scenario
- A user enters a client ID in a UserForm and wants to display the attendance date from the last matching worksheet row.
- Observed behavior
- The desired result is to populate the form with the matching client ID and a formatted attendance date, while handling missing clients and blank or invalid dates.
Use a macro-enabled workbook (.xlsm) in desktop Excel, and confirm the worksheet name, first data row, and UserForm control names. This example assumes client IDs are in column D, attendance dates are in column K, and newer records are appended below older records.
Search backward for the client's last row and populate the UserForm
Use this approach when the latest attendance record means the bottommost row matching the entered client ID.
Finding the last nonempty cell in column D identifies the search boundary, not necessarily the selected client's latest record. Search upward from that boundary until you find the client's ID, then read column K from the same row.
Press Alt+F11 to open the Visual Basic Editor, then open your UserForm. Add or select two TextBox controls and set their (Name) properties to txtClientID and txtAttendanceDate. Add a CommandButton named cmdLoadLatest so the user can enter an ID and click to load its date.
Double-click cmdLoadLatest in the UserForm designer and place this code in the UserForm module. Replace Attendance with your worksheet's tab name, and change firstDataRow if your data does not begin on row 2. Private Sub cmdLoadLatest_Click() Const firstDataRow As Long = 2 Dim ws As Worksheet Dim lastRow As Long, r As Long Dim clientID As String Dim cellID As Variant, attendanceDate As Variant Me.txtAttendanceDate.Value = "" clientID = Trim$(CStr(Me.txtClientID.Value)) If Len(clientID) = 0 Then MsgBox "Enter a client ID first.", vbExclamation Me.txtClientID.SetFocus Exit Sub End If Set ws = ThisWorkbook.Worksheets("Attendance") lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row For r = lastRow To firstDataRow Step -1 cellID = ws.Cells(r, "D").Value2 If Not IsError(cellID) Then If StrComp(Trim$(CStr(cellID)), clientID, vbTextCompare) = 0 Then Me.txtClientID.Value = CStr(cellID) attendanceDate = ws.Cells(r, "K").Value If IsError(attendanceDate) Then MsgBox "The latest matching row contains a date error.", vbExclamation ElseIf Len(Trim$(CStr(attendanceDate))) = 0 Then MsgBox "The latest matching row has no attendance date.", vbInformation ElseIf IsDate(attendanceDate) Then Me.txtAttendanceDate.Value = Format$(CDate(attendanceDate), "mm/dd/yyyy") Else MsgBox "The latest matching row contains an invalid date.", vbExclamation End If Exit Sub End If End If Next r MsgBox "No matching client ID was found.", vbInformation End Sub
Open the UserForm, enter a client ID with multiple records, and click cmdLoadLatest. Confirm that txtAttendanceDate shows column K from the bottommost matching row. Repeat with an unknown ID and a matching row containing a blank or invalid date to check the messages.

Review Excel Attendance Workbooks in WPS Spreadsheet
WPS Office is a free, lightweight alternative for reviewing attendance data with a familiar spreadsheet interface. Its Excel format compatibility makes migration straightforward for everyday worksheet work, but it does not fix Excel VBA code; VBA and UserForm support depends on the WPS edition and platform.

Frequently Asked Questions
Why does finding the last used row in column D return the wrong client?
ws.Cells(ws.Rows.Count, "D").End(xlUp).Row finds the bottommost populated cell in column D across all clients. Use that row as the starting point, then loop upward and compare each column D value with the ID entered in txtClientID.
How can I change the date format shown in the TextBox?
In the UserForm button code, change the format string in Format$(CDate(attendanceDate), "mm/dd/yyyy"). For example, use "yyyy-mm-dd" for an ISO-style date or "mmm d, yyyy" for a month-name display.
What if the last matching row has no date but an earlier row does?
The example leaves txtAttendanceDate empty and displays a message because it returns the last matching record. To return the last matching record with a valid date instead, continue searching upward after a blank or invalid date and exit only after assigning a valid date.
How do I preserve client IDs with leading zeros?
Store IDs in column D as text, such as 00123, and enter the same text in txtClientID. Formatting the numeric value 123 to display as 00123 does not change its underlying value, so the example's Value2 comparison will not match it to the text 00123.
Will this UserForm work in Excel for the web or WPS Spreadsheet?
Excel for the web cannot run VBA UserForms; open the workbook in desktop Excel to use the button code. WPS VBA and UserForm support varies by edition and platform, so verify support before relying on the form there. You can review the worksheet data without running its macros.




