I have a worksheet with form control buttons. At the end of a macro called by one button I would like to set the focus to another button, so that if I see that the first button did the job, I could just hit Enter.
I searched the site and got this
ActiveSheet.Shapes("CommandButton1").Select
This selects the shape but Enter does not run it.
Methods of CommandButton do not work.
ActiveSheet.Shapes("CommandButton1").SetFocus
returns the "method not supported" error.
I got this other answer:
Me.CommandButton1.SetFocus
I get "Object needed" error.
This leads me to believe that either CommandButton# object does not exist in Excel VBA or that it refers to a form but my buttons are placed directly on the spreadsheet.
Tried
ActiveSheet.CommandButton1.SetFocus
ActiveSheet.Buttons("Button 1").SetFocus
I found an old sample code with something like this:
CommandButton2.Caption = "CONTINUE"
CommandButton1.Enabled = False
CommandButton3.Enabled = False
This works in that sample, but in that sample the buttons behave differently. I suspect they are ActiveX or something else instead of form control buttons. In that sample right-clicking on them does not bring up the context menu.
I tried
ActiveSheet.Buttons("Button 1").ControlFormat
But do not know what to use next. SetFocus does not work.
With an ActiveX control button named "CommandButton1", you can call
Me.CommandButton1.Activate
from the same worksheet.
On a form control button named "CommandButton1", you can call
UserForm1.CommandButton1.SetFocus
This will make sure the focus is on the CommandButton1.
Related
All the programming I have learnt is through manuals and websites such as this. So I wouldn't call myself a programmer but I do use a lot of quick and dirty macros to make my life easier. Occasionally, it all falls down and assistance is required!
I have a workbook that summarises the jobs I have worked on. In order to work quicker I have added a few command buttons too speed up simple tasks like filtering the blanks in a specific column. Normally this works OK. It is three weeks since I last used the workbook and today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
I have put a breakpoint in the code. On first click the breakpoint is not reached. On second click the breakpoint is reached!
Interestingly, if I click the command button the first time and then go into the vba editor and click on the break button the ribbon returns to normal again. So something is happening but it doesn't get as far as CommandButtonIssd_Click.
The command button code looks like this:
Private Sub CommandButtonIssd_Click()
FilterIt 9
End Sub
Most of the other command buttons just act on a different column number.
In the main module the code is:
Sub FilterIt(FieldNo As Integer)
'
' Filter Macro
'
FilterOff
ActiveSheet.Range("A4").AutoFilter
ActiveSheet.Range("A4").AutoFilter Field:=FieldNo, Criteria1:="="
End Sub
Sub FilterOff()
Dim a As Range, b As Range, iRow As Integer, iCol As Integer
If ActiveSheet.AutoFilterMode Then 'on, turn it off
Set a = Selection 'keep current location
iRow = a.Row
iCol = a.Column
Selection.AutoFilter
Range(Cells(iRow, iCol), Cells(iRow, iCol)).Select 'reset current location
End If
End Sub
All this does is filter column 9 to show only the blank cells.
I am thinking that it's a Windows10, Office365 issue as the code has been stable for a while and it was working OK last time I used it. In which case, it's just a case of waiting for MS to sort it's bugs out and issue an update (every Thursday, regular as clockwork!).
OR my code is suspect and needs tweaking to stop it messing with the system.
You thoughts would be appreciated.
I had this same problem today with an Excel 365 workbook: All CommandButtons would not fire on first click. They would fire on subsequent clicks, however.
today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
In my case, the bad behavior disappeared when I closed and reopened Excel and then reopened the workbook. After that, CommandButtons fired upon first click. I concur with #MikeK that it appears to be an Excel bug.
Finally!
Same problem, different workbook.
New laptop, Windows 11, Office 365 and I think I have a better definition of the problem as a little more information is now available.
If I open the spreadsheet on the laptop screen the macro and command buttons on the spreadsheet all work fine.
If set up the monitor screen as a duplicate of the laptop screen it all works OK.
If I set up the monitor screen as an extended display, then move the spreadsheet to the monitor screen and try again it fails as follows:
Open the macro sheet and it loads OK on the monitor screen but the form with the menu on it is displayed on the laptop screen!?
Exit the macro and it takes me to the macro spreadsheet and three command buttons as it should do.
Click on "Open Contact List" and the spreadsheet content is replaced by plain black (or the contents of the underlying window!). Click again without moving the mouse and the (now invisible) button works and loads the contact list.
close the contact list and the excel window continues to display incorrectly as above.
click inside the excel window and all is restored.
same applies to the other two buttons.
Shouldn't all this be handled by the windows system?
Previously I could deal with having to click a button twice to make it work. Now I have to click an invisible button!
If it is a Windows bug, how do I report it?
I am creating a userform using Excel VBA that is meant to be used to register some sales. The form looks like this:
As you may have noticed, I am using an image as a button. This is because the CommandButton included in VBA looks very outdated. However, using an image as a button also creates me a new error (or not, depending on how you see it) that is driving me crazy. The usual process for filling this is entering a product, a quantity, a price, a customer and clicking the button to save all the information to a worksheet. The payment textbox is only filled sometimes.
I created a data validation mechanism for all these fields, including the customer combobox. If the user types an invalid entry or leaves the field empty after clicking it, a message box appears. The code for that is the following:
Private Sub cmbCustomer_AfterUpdate()
If cmbCustomer.ListIndex > -1 Then
Else
MsgBox "Please choose a valid customer", vbExclamation
cmbCustomer.Value = ""
End If
End Sub
This works great for avoiding invalid entries. The tricky part is that, once the button is clicked, all the fields are automatically erased. The data is correctly saved, but if the last field used before clicking was cmbCustomer (or any other, actually, because all of them have a similar mechanism to avoid empty or invalid data) and the user decides to begin filling the form again starting by the product, the message box appears, because it is empty and the code detects the invalid entry. I know this is the expected behavior for my code, but this doesn't happen if I use a traditional CommandButton because when clicking it the focus goes to said button. If I use my image-based button the focus remains on the last text field used before clicking it.
One solution would be to override the message box in this specific situation (when saving the data). The second one would be to reset the focus of the form, or set focus to the image like what happens with a regular CommandButton. Any suggestions would be greatly appreciated.
You can do yourself what the traditional CommandButton does automatically: Set the focus where you want it:
Private Sub cmbCustomer_AfterUpdate()
If cmbCustomer.ListIndex > -1 Then
Else
MsgBox "Please choose a valid customer", vbExclamation
cmbCustomer.Value = ""
myButton.SetFocus
End If
End Sub
If SetFocus doesn't work, be mindful of where you are in the UI event chain: Why is my .setfocus ignored?
As mentioned in the comments, an image button can't acquire the focus. A transparent CommandButton behind it can be used as a proxy.
So I have a Form Control Button NOT and ActiveX Command Button that I want to make disappear when another button is clicked. Normally if it was a ActiveX Command Button i would use:
ButtonName.Visible = False
in the code that was triggered by the other button. However I dont know how to get that work with a Form Control Button as they dont have name like a ActiveX Command Button does so far as I know?
I would just change the button I want to disappear to a ActiveX Command Button but when I do that I have issues with the buttons format changing, text sizes up when they are clicked, buttons change size when clicked, etc.
You may also try
Sub Button2_Click()
Sheets("Sheet1").Buttons("Button 1").Visible = False
End Sub
.Shapes("button 1").Visible = False
I can't get the RowSource property of a list box to update via VBA. From another thread, I found the syntax, so I think this is correct. But, despite not failing, it doesn't do anything to the RowSource property (it remains blank). Below:
frmAddIngredient is the user form.
lbxIngredient is a listbox control in that form.
UniqueIngredients is one of the sheets in the workbook.
NumberOfItems is 1 (in this case).
It doesn't give an error, but it doesn't change anything, either. The form itself is not active at this time. This code is supposed to set up the form for later showing.
frmAddIngredient.lbxIngredient.RowSource = Sheets("UniqueIngredients").Range("A1:A" & CStr(NumberOfItems)).Address
The most recent code is
frmAddIngredient.lbxIngredient.RowSource = "=UniqueIngredients!A1:A1"
but, it still doesn't change anything in the actual form.
Also, can I add a new post, or do I have to continue editing this one and adding stuff?
What you want (as discussed in the comments on your question) is not possible. Setting something by code does not change it's property in the properties window and is only so until your project resets.
Consider a Userform with 2 buttons, with their original name and caption and then in a module paste these 2 subs.
Sub demo1()
UserForm1.CommandButton1.Caption = "Demo 1"
UserForm1.Show
End Sub
Sub demo2()
UserForm1.CommandButton2.Caption = "Demo 2"
UserForm1.Show
End Sub
When you run the first Sub demo1 Button 1's caption has changed but Button 2's caption has not.
Close the Userfom and now run demo2, you'll see that Button 1's caption is back to it's original hard set (properties window) name and that now Button 2 has a different name.
I know how to assign a macro to a cell and launch when it has changed.
I know how to assign a macro to a 'button' and launch it when clicked.
I know how to assign a macro to a worksheet and launch when any cell has changed.
But now ..... I have a text box in a chart and wish to have a macro launch AFTER the text box has been moved to a new location. Currently, I click on a 'button' after I move the text box and this works well. I would like to eliminate the final step in order to avoid a problem if the user fails to click the button.
If there is no built-in Event associated with a worksheet property or characteristic you wish to monitor, you can always use an Application.OnTime macro to monitor the item.
For example the TopLeftCell.Address of a Shape can be monitored from call to call and action taken if the property changes.
Some some information see OnTime
I think you can found what you want Here.