David Phelops
asked on
How to exit a sub from a userform "Cancel_Click"
I have code which loads a user form.
The userform displays a list of clients and asks for "ok" or "cancel"
ok continues the routine
cancel should exit the routine.
I'm aiming for this:
If the userform result is ok then continue
else if the userform result is cancel, then end the routine.
?
How does the syntax go?
My code so far within the routine is:
'Load the userform with Clientlist information
ufClientList.Show
__________________________ __________ __________ __________ __________ __
Then, the code for the Userform is:
Private Sub cmd_Cancel_Click()
Unload Me
End Sub
__________________________ __________ __________ __________ __________ ___
Private Sub cmd_Ok_Click()
Unload Me
End Sub
__________________________ __________ __________ __________ __________ ____
Private Sub UserForm_Initialize()
'Check that data exists in the correct sheet
If Not Range("A7") Like "acct*" Then
MsgBox "There are no client data"
Exit Sub
Else
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).Name = "Clients"
Me.Lbox_ClientList.RowSour ce = Range("Clients").Address
End If
End Sub
__________________________ __________ __________ __________ __________ ____
So far i have found that either clicking OK, or Cancel leads to the routine continuing.
What is the bit of code I have missed to make it all go away?
Thanks very much!
The userform displays a list of clients and asks for "ok" or "cancel"
ok continues the routine
cancel should exit the routine.
I'm aiming for this:
If the userform result is ok then continue
else if the userform result is cancel, then end the routine.
?
How does the syntax go?
My code so far within the routine is:
'Load the userform with Clientlist information
ufClientList.Show
__________________________
Then, the code for the Userform is:
Private Sub cmd_Cancel_Click()
Unload Me
End Sub
__________________________
Private Sub cmd_Ok_Click()
Unload Me
End Sub
__________________________
Private Sub UserForm_Initialize()
'Check that data exists in the correct sheet
If Not Range("A7") Like "acct*" Then
MsgBox "There are no client data"
Exit Sub
Else
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).Name = "Clients"
Me.Lbox_ClientList.RowSour
End If
End Sub
__________________________
So far i have found that either clicking OK, or Cancel leads to the routine continuing.
What is the bit of code I have missed to make it all go away?
Thanks very much!
ASKER
Thanks very much for the quick response...
I have change my code to yours:
When the Cancel button is clicked now a "Compile Error: Syntax Error" message is displayed.
Please see screenshot attached...
Have I missed something.....?
Cheers
Compile-Error-VBA.docx
I have change my code to yours:
When the Cancel button is clicked now a "Compile Error: Syntax Error" message is displayed.
Please see screenshot attached...
Have I missed something.....?
Cheers
Compile-Error-VBA.docx
You are missing a closing parenthesis after the second Cells(8, 1) in your Initialize sub.
ASKER
The second parenthesis is there, otherwise the userform would not work at all, even when the OK button was clicked. I'd get the message: "Expected List Separator )"
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).Name = "Clients"
i'm very puzzled...
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).Name = "Clients"
i'm very puzzled...
Sorry - didn't look at the picture closely enough. Would be much easier to figure out with a workbook (you can remove all data).
ASKER
Thanks... I have attached the file minus all sensitive data.
Most appreciated.
Cheers
David
SampleExcelFile.xlsm
Most appreciated.
Cheers
David
SampleExcelFile.xlsm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good Grief... as if it isn't hard enough without blank lines conspiring against us....
Thanks so much for your help.
Cheers
David
Thanks so much for your help.
Cheers
David
Open in new window
then alter the button codes to:
Open in new window
then your calling code becomes:
Open in new window