Command Button doesn't work when first clicked - excel

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?

Related

Running Excel 4.0 macro using button

Macros on my spreadsheet that have been working for years, stopped working via buttons, though they still work from the VBA Developer window.
I use a normal shape (rectangle) as the button and assigned the macro to it (selecting from "ThisWorkbook").
To head off suggested fixes I've seen for other similar posts:
It is still an .xlsm file
Macros are still enabled.
It's running on the same laptop as before (Windows 10, Office 365).
No Windows updates have occurred lately.
I only have this one file open.
I tried rebooting laptop and restarting Excel.
I'm not using an Active-X Control.
I tried it with a Form Control button and a regular Shape button - neither work.
It's not related to the actual VBA code (see below for proof).
I created a new program to show the problem is not the code itself:
Sub button_not_working()
MsgBox "button_not_working"
End Sub
This program works using the green Play button in the VBA screen, but not via an assigned button on a sheet.
Code is in a normal VBA code Module (not "ThisWorkbook" area on VBA screen).
Macro is assigned by right-clicking shape, and the list of available Macros is just those in "This Workbook" on the Assign Macro popup.
When clicking the Shape to run the assigned macro I get this error message.
You can't call code from ThisWorkbook in a button event.
I like to put the button events in the code behind the sheet where the button lives. This way the code move with the sheet wherever that sheet is copied.
Use a form button. Right-click on the form button and select Assign Macro.... Then select the VBA subroutine from the list that pops up. Only procedures visible on this popup will work.
NOTE:
Never use _ in any names in VBA. It's reserved for event handling.
Public Sub ButtonIsWorking()
MsgBox "button is working"
End Sub
Code in sheet:
Assign Macro:
Button press:
Often this issue can occur due to multiple screens or resolution difference issues. If you are using a laptop connected to screens try using the button on the laptop while it's disconnected from your monitors. If this fixes the issue then ensure you have the same resolution and/or scaling between the two setups.
If this change only happened recently and is on a desktop or laptop screen without additional monitors ensure your scaling is set to 100% in case it has recently changed.
Solved - turned out I needed to check the box for "Enable Excel 4.0 Macros".
Apparently, Microsoft has just started disabling this by default in Office 365 starting this month!
So in Excel... File>>Options>>Trust Center>>Trust Center Settings>>Macro settings
Something to try: Make sure your 'caller' shapes have unique names
Note: It's possible to have multiple shapes with the same name and (for whatever reason) that can confuse excel's shape-to-macro-connection.
Additional Info 1:
If you use a 'grouped shapes' object as a control, you should assign the same name to all shapes in the group.
Additional Info 2:
The original OP symptom was simply "Macros ... stopped working via buttons." The OP has since been updated, and shows an error message. The disconnect-due-to-non-unique-naming described here doesn't elicit an error message. Rather the symptoms can be either of: a) the macro isn't run at all or b) the 'caller' object in the called macro is invalid.

Excel macro fails when linked to a shape but works using a command button [duplicate]

Macros on my spreadsheet that have been working for years, stopped working via buttons, though they still work from the VBA Developer window.
I use a normal shape (rectangle) as the button and assigned the macro to it (selecting from "ThisWorkbook").
To head off suggested fixes I've seen for other similar posts:
It is still an .xlsm file
Macros are still enabled.
It's running on the same laptop as before (Windows 10, Office 365).
No Windows updates have occurred lately.
I only have this one file open.
I tried rebooting laptop and restarting Excel.
I'm not using an Active-X Control.
I tried it with a Form Control button and a regular Shape button - neither work.
It's not related to the actual VBA code (see below for proof).
I created a new program to show the problem is not the code itself:
Sub button_not_working()
MsgBox "button_not_working"
End Sub
This program works using the green Play button in the VBA screen, but not via an assigned button on a sheet.
Code is in a normal VBA code Module (not "ThisWorkbook" area on VBA screen).
Macro is assigned by right-clicking shape, and the list of available Macros is just those in "This Workbook" on the Assign Macro popup.
When clicking the Shape to run the assigned macro I get this error message.
You can't call code from ThisWorkbook in a button event.
I like to put the button events in the code behind the sheet where the button lives. This way the code move with the sheet wherever that sheet is copied.
Use a form button. Right-click on the form button and select Assign Macro.... Then select the VBA subroutine from the list that pops up. Only procedures visible on this popup will work.
NOTE:
Never use _ in any names in VBA. It's reserved for event handling.
Public Sub ButtonIsWorking()
MsgBox "button is working"
End Sub
Code in sheet:
Assign Macro:
Button press:
Often this issue can occur due to multiple screens or resolution difference issues. If you are using a laptop connected to screens try using the button on the laptop while it's disconnected from your monitors. If this fixes the issue then ensure you have the same resolution and/or scaling between the two setups.
If this change only happened recently and is on a desktop or laptop screen without additional monitors ensure your scaling is set to 100% in case it has recently changed.
Solved - turned out I needed to check the box for "Enable Excel 4.0 Macros".
Apparently, Microsoft has just started disabling this by default in Office 365 starting this month!
So in Excel... File>>Options>>Trust Center>>Trust Center Settings>>Macro settings
Something to try: Make sure your 'caller' shapes have unique names
Note: It's possible to have multiple shapes with the same name and (for whatever reason) that can confuse excel's shape-to-macro-connection.
Additional Info 1:
If you use a 'grouped shapes' object as a control, you should assign the same name to all shapes in the group.
Additional Info 2:
The original OP symptom was simply "Macros ... stopped working via buttons." The OP has since been updated, and shows an error message. The disconnect-due-to-non-unique-naming described here doesn't elicit an error message. Rather the symptoms can be either of: a) the macro isn't run at all or b) the 'caller' object in the called macro is invalid.

Excel Crashes Intermittently When Clicking Macro Button in Rapid Succession

When a VBA macro button (not AcitveX button) is clicked in rapid succession Excel "sometimes" crashes.
The VBA code makes heavy use of object modules, so I was thinking it was a garbage collection issue. I explicitly set the top level object to nothing before exiting the button click macro thinking it would force a garbage collection. That did not work.
It is super frustrating because it is intermittent. Maybe 1 out of 10 to 20 times.
The code shown is just the button click handler. There is about 10,000 lines of code called from this handler, which I did not show. The VBA code reads information from the sheet, does some calculations, updates an excel chart on the sheet, and writes some data back to the worksheet. I do the usual turning off events and screen updates.
I am just hoping someone else has come up against the rapid macro execution causing excel to crash. Again, the VBA code runs fine, it appears to be a higher level excel issue?
Public Sub Clicked_UpdateWall_C()
Dim Wall As New CWall_C
Dim ExecutionSuccess As Boolean
Dim errstr As String
ExecutionSuccess = CheckUnits(ActiveSheet.Name, errstr)
If ExecutionSuccess Then ExecutionSuccess = Wall.UpdateWall(ActiveSheet.Name, errstr)
Call CheckError(ExecutionSuccess, errstr)
' This is an attempt to force excel to do garbage collection
Set Wall = Nothing
End Sub
The error message is "Excel has stopped working" not a VBA runtime error. One can click the "restart excel" button in the error dialog, and excel restarts and generally most of the time one does not lose work.
Since it is intermittent, I cannot post the exact excel crash dialog box text.
When a VBA macro button (not AcitveX button) is clicked in rapid succession Excel "sometimes" crashes.
A shot in the dark. Try this. Put your code in lieu of '~~> Rest of your code. Now no matter how many times you click in succession, nothing will happen.
Option Explicit
Dim DoNotRunProc As Boolean
Public Sub Clicked_UpdateWall_C()
If DoNotRunProc = True Then Exit Sub
DoNotRunProc = True
On Error GoTo Whoa
'
'~~> Rest of your code
'
Whoa:
DoNotRunProc = False
End Sub
Note: If you have a separate error handler in your code then adjust the above code accordingly.
I was able to resolve the issue by doing two things:
Switched from a "Button Form Control" to an "Command Button ActiveX Control." My hunch was that the ActiveX control is more robust in terms of handling rapid clicks. Turned out to be true.
Added the DoEvents function to the end of the Command Button ActiveX Control event handler. This pretty much eliminated the issue 99.9% unless someone is just being ridiculous clicking the button. The hunch here is that it gave Excel time to handle any outstanding events that perhaps were not handled properly due to rapid button clicks.
Thanks to all of you who responded with positive comments and suggestions.

Press OK on pop-up alert window via VBA

Having an issue, I have written a Macro ,that refreshes every 2 min, to upload data from a webpage and the last step of my Macro is using "Text to column" function so that it will be splitted into columns nicely.
The problem appears here everytime it gets to executes this "Text to column" it asks if I want to overwrite the columns(and I DO), but I need to press manually OK.
Is there any way to make VBA press this OK button in the alert?
Thank you very much!
Just add Application.DisplayAlerts = False prior to the call, and set it back to True after.
http://msdn.microsoft.com/en-us/library/office/aa175241%28v=office.11%29.aspx
Update: I just ran a test and it does not prompt me by default. Can you post the code you're using? I simply did:
Public Sub Test()
Dim r As Range
Set r = ActiveWorkbook.Sheets(1).Columns("A:A")
r.TextToColumns Destination:=Range("B1")
End Sub
This link says the DisplayAlerts solution has worked in for others.
You can add a DoCmd.SendKeys BEFORE the instruction that generates the dialog.
You should use mouse click function
Before it maximize window so x,y cordinates of ok buttons remain same
Use windows lib 32

excel 2007 vba Application.Dialogs(xlDialogPrint).Show crashes if user chooses print preview

In Excel 2007, have a button that triggers a macro, which selects a few sheets out of many and sends to Application.Dialogs(xlDialogPrint).Show. As part of selecting the few sheets there are other macros triggered like showing certain rows, password protecting and unprotect-ing, etc.
It prints and cancels fine, except when users click the 'print preview' button in that printer dialog box. It shows the printer dialog fine, but no matter if they hit print or close it crashes.
It appears to run through the whole macro a second time and crashes because expected values and settings are not in place like normal when it runs through the first time.
Any way to account or or capture the print preview dialog stuff when print preview is launched via the printer dialog from Application.Dialogs(xlDialogPrint).Show?
I have tried changing passed parameters like
Application.Dialogs(xlDialogPrint).Show ,,,,,False
Application.Dialogs(xlDialogPrint).Show Arg6:=False
But these are not working; I've read that you can't alter the dialog anyway.
(Hope this is clear)
Not sure if you want to show print preview directly from the button click or not. Usually I use something like this. I find it easier to view the preview first, then decide if I want to print out a hard copy. But it might not work for your situation.
Private Sub CommandButton1_Click()
Dim vSheets() As Variant
vSheets = Array("Sheet1", "Sheet2")
ActiveWorkbook.Sheets(vSheets).Select 'sheets need to be selected
ActiveWorkbook.PrintOut preview:=True 'brings up print preview
End Sub

Resources