I have a project in VBA in which I am already preparing the outputs for errors and there is an error that can be generated in the initialization of a UserForm .
Private Sub UserForm_Initialize()
With Application.WorksheetFunction
On Error GoTo initiate
'Acá puede ocurrir un error
nameNUIP.Caption = .VLookup(...) _
+ " " + .VLookup(...) 'Lo pongo así porque no es necesaria la información
BirthDate.Caption = .VLookup(...)
RegDate.Caption = .VLookup(...)
Place.Caption = .VLookup(...) _
+ ", " + .VLookup(...)
On Error GoTo 0
End With
Exit Sub
'Control de errores
initiate: 'Acá se viene el código si ocurre el error
MsgBox Title:="NUIP ERROR:" & Err.Number, _
Prompt:="Fallo en la búsqueda del NUIP." & vbNewLine & "¿Seguro que ya se registró?", _
Buttons:=vbOKOnly + vbExclamation
End Sub
When the code passes to the initiate
tag by not finding data, the window with the message appears as expected, but after accepting, the macro continues and shows the UserForm , which I do not want. I wish that if the error occurs, stop running the macro, but do not close the book.
Is it possible to close everything if an error occurs? What methods could you use for this?
PS: My version of Excel is 2016.