Form Button Clicks in Excel 2010 - I Thought I Got It
Posted
Sun, Feb 13 2011 21:44
by
Nate Oliver
Tonight's entry is something different, as I'll write about something I don't understand.
So, here's the scenario. Open a new Workbook, paste in the following procedure:
Sub baz()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
You can save the Workbook first, if you like. Tie it to a Form button and click. On my machine, this crashes Excel 2010 every time.
I thought I knew what the issue was: Focus. The Shape is taking focus during the running of the Procedure; so, I built a helper in the queue. Something simple, so that the Form Button calls something like this (foo() ):
Sub foo()
Application.OnTime Now, "bar"
End Sub
Sub bar()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
That works every time - and that makes sense; the Shape doesn't have focus as bar() runs.
But, here's where things get strange. If you close the saved file with baz() directly assigned to the button, whereas it killed Excel the first time you ran it, it works fine on subsequent openings. So, apparently, it's not a focus issue, and one I don't understand. Excel is somehow confused on the initial open and corrects itself on the 2nd opening of the file.
Regards, Nate