What's the code for 'if commandbutton1 is clicked?

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hi All,

I has an exit event in a textbox on a userform that sets focus on a combobox.

I have a 'Exit' button on the userform as well.

I simply need to say in the textbox exit event to setfocus on the combobox, unless the exit button is pressed.

Something like

Code:
private sub textbox1.exit (ByVal Cancel As MSForms.ReturnBoolean)
 
if not commandbutton1.click = True then
combobox1.setfocus
end if
 
end sub

But I don't know what the correct way to say commandbutton1.click = true is.

Thoughts ladies and gentlemen?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi there,

Try...

Code:
Application.Caller

...which will return the name of control clicked, except for labels.

Robert
 
Upvote 0
If the Exit button closes the userform, why does it matter where the focus is?

Also, if the CommandButton1's TakeFocus******* property is True, the Exit event will run before the command button's code is run.

There is no way for the Exit event to tell why the textbox is being exited.

If it does matter, the focus could be controlled in the first line of the CommandButton1_Click event.
 
Upvote 0
Hi Mike,

Not totally following you yet...

To answer your question, the problem is that when I click on the exit button the setfocus overides the buttons code and instead of the userform closing, the focus simply moves.
 
Upvote 0
Trebor,

The value of Application.Caller depends on how normal excel was left when going into VBA.

If the userform is invoked by a control on a worksheet, it will return the name of that control.
If the UF is invoked by F5 from the VBEditor,it will return an error.
If by a worksheet function, it will return the calling cell.

It will not return the value of a control on a userform.
 
Upvote 0
Set the CommandButton's .TakeFocus******* property to False and all will be well. (Unless the user clicks on the corner X, in which case there is an error because the code is trying to set the focus to an invisible ComboBox.)

It might be best if you were to just change the Tab Order of the userform so that the ComboBox comes after the text box.

Or use the AfterUpdate event of the textbox.
 
Last edited:
Upvote 0
Sounds like tab order might be the one... I've already been over the tabs once but these are at the start... can you use negative numbers -3 -2 -1 is tab order?
 
Upvote 0
Actually, setting the button's takefocus seems to work well. What did you mean that the combobox would be invisible?
 
Upvote 0
In the VBEditor under the View Menu, select Tab Order and use that to put things in the order you want.

No you can't use negative numbers for the TabIndex.
From VBEditor Help:
TabIndex:...An integer from 0 to one less than the number of controls on the form that have a TabIndex property. Assigning a TabIndex value of less than 0 generates an error. If you assign a TabIndex value greater than the largest index value, the system resets the value to the maximum allowable value.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top