Home » excel » excel – Debugging "Search for Name" Code in VBA

excel – Debugging "Search for Name" Code in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have solid experience in C++ but am still getting used to the syntax of VBA and I think that’s what’s tripping me up in my code.

What I’m trying to do is have a button that asks the user for a name. If the name entered is in column B, then tell the user the name was found and select where it is (no problem with this). If the name is not found, then ask if the user wants to try another name (no problem with this, either).

Where I’m having trouble is with the “Cancel” buttons. At any time, I want the user to be able to hit “Cancel” and immediately stop the loop, but stay in the sub because I’ll be adding to this later.

Here’s the code:

Dim inputName As String
Dim row As Integer
Dim i As Integer
Dim tryAgainResponse As Integer
    tryAgainResponse = vbOK

'Ask user for name they would like to replace'
inputName = InputBox("What is the name of the person you would like to find? (First Last)")

'Find the row that the name is located and tell the user where it is'
Do While tryAgainResponse = vbOK
    For i = 1 To 1000
        If Cells(i, 2).Value = inputName Then
            MsgBox ("Found the name! It's located at cell B" & i & ".")
            ActiveSheet.Cells(i, 2).Select
            tryAgainResponse = 0
            Exit Do
        End If
    Next i
    tryAgainResponse = MsgBox("We didn't find the name you were looking for. Please try again.", vbOKCancel)
    If tryAgainResponse = vbCancel Then
        Exit Do
    End If
    inputName = InputBox("What is the name of the person you would like to find? (First Last)")
Loop

I’ve tried plenty of things, but the main error is when you hit cancel for the first MsgBox, it tells you the name was found in the first blank square.

Any help or suggestions would be greatly appreciated! This is my first VBA program, so it’s not the prettiest, but it’s definitely a lot of fun. Thanks!

How to&Answers:

I’m not sure if I’m understanding what you’re asking for, and I can’t comment for clarification, but I think your hang up is that when you click cancel on the INPUT box, your input box is returning a blank string, and the rest of your code is then finding a blank cell.

Use the Application.Input Method, declare your input string as a variant, and test if it is false. If it is, use an Exit Sub to exit the macro. You could also test if your input string = “” and then exit the macro if true with the code you have.

From MrExcel

There are 2 versions of InputBox in VBA.

The InputBox Function is called without an object qualifiier and returns the contents of the text box or a zero-length string (“”) if the user clicks Cancel.

The InputBox Method is a member of the Application object, so it is called by using Application.InputBox. It returns the contents of the text box or False if the user clicks Cancel. It is more versatile than the InputBox Function because it has a Type argument which specifies the return data type.

Answer:

The function InputBox() will return an empty string if cancelled. The empty string will compare equal to the first cell that is empty.

Here’s the doc of the function: http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx