lela0643
12-02-2022, 02:45 AM
Exercise: Find a student
Open the workbook "Résultats examen.xlsx". In the first spreadsheet (Results) you have a list of students.
1) Build a VBA macro that asks the user with a dialog box, the name of the student to find. This macro will browse the list and select the cell corresponding to the name of the student.
2) The macro informs the user, with a second dialog box, if the student has been received.
3) If all the list has been explored and the name has not been found, the macro displays the message “student not found in the list” and select the first empty cell below the names list.
Can anyone help me? So far I have this but it does not work
Sub Search()
Dim fnd As Variant
Dim FoundCell As Range
Dim rng As Range
Do
fnd = InputBox("Enter Student Name To search", "Enter Name")
If StrPtr(fnd) = 0 Then Exit Sub
Loop Until Len(fnd) > 0
Set FoundCell = Range("A:A").Find(fnd, , xlValues, xlPart, , , False, , False)
If Not FoundCell Is Nothing Then
FoundCell.Select
MsgBox fnd & Chr(10) & "was found in the list", 48, "Found"
Exit Sub
End If
MsgBox fnd & Chr(10) & "student not found in the list", 48, "Not Found"
Set rng = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
rng.Select
End Sub
Open the workbook "Résultats examen.xlsx". In the first spreadsheet (Results) you have a list of students.
1) Build a VBA macro that asks the user with a dialog box, the name of the student to find. This macro will browse the list and select the cell corresponding to the name of the student.
2) The macro informs the user, with a second dialog box, if the student has been received.
3) If all the list has been explored and the name has not been found, the macro displays the message “student not found in the list” and select the first empty cell below the names list.
Can anyone help me? So far I have this but it does not work
Sub Search()
Dim fnd As Variant
Dim FoundCell As Range
Dim rng As Range
Do
fnd = InputBox("Enter Student Name To search", "Enter Name")
If StrPtr(fnd) = 0 Then Exit Sub
Loop Until Len(fnd) > 0
Set FoundCell = Range("A:A").Find(fnd, , xlValues, xlPart, , , False, , False)
If Not FoundCell Is Nothing Then
FoundCell.Select
MsgBox fnd & Chr(10) & "was found in the list", 48, "Found"
Exit Sub
End If
MsgBox fnd & Chr(10) & "student not found in the list", 48, "Not Found"
Set rng = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
rng.Select
End Sub