Command Button question

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

Is there a way to establish whether a command button has been pressed

I wish to execute some code on exiting one control only if a particular
command button had been pressed

Example

Private Sub cbRestaurant_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If command Button pressed Then
Do This
Exit Sub
End If

more code here

End Sub

Regards & TIA
 
You could write a 1 or 0 value out to a cell in an unused worksheet and then
check that value to verify if the command button has been pressed before
executing your code..
 
Thanks for the suggestion Kevin I will try it tomorrow,

I don't think I explained my requirements very well.

What I am trying to achieve is probably not possible
In essence I am trying to prevent the On Exit code from running when the
Exit is caused by pressing a particular command button but allowing it to
run in all other instances

thanks again
 
Is there a way to establish whether a command button has been pressed

Use a variable with UserForm-wide scope to track the buttons pushed.
I wish to execute some code on exiting one control only if a particular
command button had been pressed

I'm thinking code something like that below my signature (it assumes 3
CommandButton Click events and a TextBox's Exit event). Every time a
CommandButton is pressed, that button's Name is stored in the
LastPressedButton variable. All you do in the Exit event is check if that
button was the last one pressed.

Rick

Dim LastPressedButton As String

Private Sub CommandButton1_Click()
LastPressedButton = "CommandButton1"
' Put rest of your code here
End Sub

Private Sub CommandButton2_Click()
LastPressedButton = "CommandButton2"
' Put rest of your code here
End Sub

Private Sub CommandButton3_Click()
LastPressedButton = "CommandButton3"
' Put rest of your code here
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If LastPressedButton = "CommandButton2" Then
Cancel = True
' Any other code you might want to execute
End If
' Your normal Exit code goes here
End Sub
 

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

Back
Top