Goto "Start of Sub"

G

Guest

Hi.

I have a button on my excel sheet that call a macro (sub).

Both in the middle and in the end of the sub a userform emerge where the
user can choose if he want to continue or start at the beginning of the sub.

My question is: Is there a command (code) (like Goto??) to go back to the
beginning of the sub?

How should i write a code that check if a txtbox on a userform is empty, and
if it is a msgbox emerge. If the user click OK on the msgbox he will get a
second (or more) chance to write in the txtbox!

Any suggestion?
 
B

Bob Phillips

Michael,

I think you are getting a bit confused on how objects work.

A textbox is an object, and there are various events associated with it. You
can only hand control to your code by trapping one of these events. Once you
have completed your action (reaction) to the event), you exit and your code
hands back to the system that will allow any other object on the userform to
be acted upon.

So what I am saying is you should either test on the textbox exit if it is
empty and then force the user back in, or (preferably IMO), have some sort
of commit button (usually called OK) which when clicked you check that all
is okay. If so, don't let them finish, but force them back to the error. And
of course you have a Quit button so that they can quit complete or not.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Hi Bob.

Thanks!

I have code that check the input in the txtbox on the userform, and return
that to a range in the sheet, but if the input in the txtbox is out of range
(ex. less than 0) a msgbox emerge and tell the user its out of range. I want
to use a error handler in the code that "start again". My point is; if the
input is out of range and the user click Ok on the msgbox he will get a
second chance.
 
B

Bob Phillips

Michael,

I would still use a button with code like

Private Sub CommandButton1_Click()
Dim ans
With Me.TextBox1
If CDbl(.Text) < 0 Then
ans = MsgBox("Invalid Amount, try again", vbOKCancel)
If ans = vbOK Then
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End If
Else
Worksheets("Sheet1").Range("A1").Value = .Text
End If
End With
End Sub

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top