VBA using Loop

lela0643

New Member
Joined
Dec 2, 2022
Messages
6
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Exercise 12: 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.

Help please?
1669974639478.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi there...

Try the below...

VBA Code:
Sub Search()
'Original Code by dmt32 06/11/2022
'Updated by Jimmypop 02/12/2022
    Dim fnd         As Variant
    Dim FoundCell   As Range
    Dim rng     As Range
    Do
        fnd = InputBox("Enter Student Name To search", "Enter Name")
        'cancel pressed
        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
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA using Loop
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Hi there...

Try the below...

VBA Code:
Sub Search()
'Original Code by dmt32 06/11/2022
'Updated by Jimmypop 02/12/2022
    Dim fnd         As Variant
    Dim FoundCell   As Range
    Dim rng     As Range
    Do
        fnd = InputBox("Enter Student Name To search", "Enter Name")
        'cancel pressed
        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
Thank you, unfortunately after putting the name into the Box it doesn't do anything...
 
Upvote 0
What is the range to search... Family Name (Range A) or First Name(Range B)?
How are you running the macro? Via a button or something else? I tested this and works on my side...

View attachment 80059
View attachment 80060
View attachment 80061
now I was able to make it work, thank you so much!!
How can I make a macro that informs the user, with a second dialog box, if the student has been received?

And another quick question what do the functions StrPtr and Chr do? I am not familiar with those and was wondering wheter they can be replaced by a more simple way?
 
Upvote 0

Forum statistics

Threads
1,215,340
Messages
6,124,382
Members
449,155
Latest member
ravioli44

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