sidalam
Programmer
- Nov 21, 2003
- 94
I am using the code below to find a student in an Excel spreadsheet however why does MsgBox("Name not found") ever get displayed. The code searches for a name in cells A4 to A14.
Private Sub CommandButton1_Click()
Dim findname As String
Dim counter As Integer
Dim cellRange As String
findname = InputBox("Enter student name")
For counter = 4 To 15
cellRange = "A" & counter
If Range(cellRange).Value = findname Then Exit For
Next counter
If counter = 15 Then
MsgBox ("Name not found")
Else
MsgBox ("Found at " & cellRange)
End If
End Sub
Private Sub CommandButton1_Click()
Dim findname As String
Dim counter As Integer
Dim cellRange As String
findname = InputBox("Enter student name")
For counter = 4 To 15
cellRange = "A" & counter
If Range(cellRange).Value = findname Then Exit For
Next counter
If counter = 15 Then
MsgBox ("Name not found")
Else
MsgBox ("Found at " & cellRange)
End If
End Sub