Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i execute this code.Its actually ignore the exit sub after the 1st msgbox and display the next msgbox whether the condition is true or false. Please help me with the answer


VB
Sub Insertnex()
Dim Name As String
Dim State As String
Dim Newdate As Date
Dim ButtonClicked As VbMsgBoxResult

GuessAgain:
Name = InputBox("Please enter the name")
Sheet1.Activate

On Error GoTo Namenotfound
Range("B3:B15").Find(Name).Select
On Error GoTo 0

Name = ActiveCell.Value
State = ActiveCell.Offset(0, 1).Value
On Error GoTo InvalidDate

Date = ActiveCell.Offset(0, 2).Value
MsgBox Name & " stays in " & State & " she is staying there since " & Date

Exit Sub

Namenotfound:
ButtonClicked = MsgBox(Name & " was not found in the list.")
 
If ButtonClicked = vbRetry Then Resume GuessAgain

Exit Sub

InvalidDate:
MsgBox Date & "Was in wrong formate"


End Sub


What I have tried:

I tried to figure out the issue but i am unable to solve this
Posted
Updated 6-Feb-18 20:37pm
v2

1 solution

To be able to get vbRetry code, you need to properly set up MsgBox, for example:

VB
Sub Test()
Dim ButtonClicked As Long

ButtonClicked = vbRetry
Do While ButtonClicked = vbRetry
    ButtonClicked = MsgBox("Click Cancel to exit the loop", vbRetryCancel, "Not found...")
Loop

End Sub


Feel free to change the code to your needs.
 
Share this answer
 
Comments
Member 13658733 7-Feb-18 4:02am    
Thanks for your Suggestion Maciej Los but i want to understand why my Exit Sub is not working after 1st msgbox.
Maciej Los 7-Feb-18 6:46am    
Well... the best way to find out why your code doesn't work as expected is to use debugger. How? Use [F8] instead of [F5]. This will give a chance to resolve your issue by executing a code line by line.
Cheers,
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900