What is Wrong With Here

yasirsaeed2002

New Member
Joined
Mar 26, 2017
Messages
30
Hi Guys,

I am inexperienced in VBA. I have written the following code. The program runs once if the Name is not found but returns error 91 If I retry rather than going to Namecorrection Label. I wonder why?

Thanks in Advance
Yasir


Dim Name as String

RetryAgain:

Name = Inputbox("Type in Your Name Here")

On Error GoTo NameCorrection


Range("B1", Range("B1").End(xlDown)).Find(name).Select (Error 91 here the 2nd time)

On Error GoTo 0

NameCorrection:

MBox = MsgBox("The Name You Entered was Not Found. Do you Want To Try Again", vbRetryCancel)

If MBox = vbRetry Then GoTo RetryAgain
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi

You probably don't need to "goto" anything, try

Code:
Sub Yasir()
Dim DoAgain As Variant
Dim findName As String
Dim lstRw As Integer
Dim found As Range
Dim msg As String


lstRw = Cells(Rows.Count, 2).End(xlUp).Row


Do Until DoAgain = vbNo
    findName = InputBox("Type in Your Name Here")
    
    Set found = Range("B1:B" & lstRw).Find(findName)
    
    If Not found Is Nothing Then
        msg = "I found " & findName & " at " & found.Address
    Else
        msg = "The Name You Entered was Not Found"
    End If
    
    DoAgain = MsgBox(msg & ", go again?", vbYesNo)
Loop
End Sub
 
Upvote 0
Hi

You probably don't need to "goto" anything, try

Code:
Sub Yasir()
Dim DoAgain As Variant
Dim findName As String
Dim lstRw As Integer
Dim found As Range
Dim msg As String


lstRw = Cells(Rows.Count, 2).End(xlUp).Row


Do Until DoAgain = vbNo
    findName = InputBox("Type in Your Name Here")
    
    Set found = Range("B1:B" & lstRw).Find(findName)
    
    If Not found Is Nothing Then
        msg = "I found " & findName & " at " & found.Address
    Else
        msg = "The Name You Entered was Not Found"
    End If
    
    DoAgain = MsgBox(msg & ", go again?", vbYesNo)
Loop
End Sub

Hi Mrhstn,

Thanks for your reply. The code work perfect except when you press cancel it does not exit sub rather gives a message as follows:

I found at $B$14, go again?

Also please I would appreciate if you can comment on the problem with my code as I was doing some practice.

Kind Regards
Yasir
 
Upvote 0
Sorry, forgot about cancelling

Try

Code:
findName = InputBox("Type in Your Name Here")   
If findName = vbNullString Then Exit Do

in place of

Code:
    findName = InputBox("Type in Your Name Here")
 
Last edited:
Upvote 0
Also please I would appreciate if you can comment on the problem with my code as I was doing some practice.
The problem is with your error handler. VBA can only handle one error at a time, so if you get an error, it needs to be cleared before any other errors can be handled. have a look here for more info.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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