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

Comments

# re: Form Button Clicks in Excel 2010 - I Thought I Got It

Monday, February 14, 2011 1:29 AM by Darpan More

Thanks Nate. Genius, you are a great help. Your work around works for me, I will test it still. I was surprised with this because 2007 version did not give me any such error. Thank You!

# re: Form Button Clicks in Excel 2010 - I Thought I Got It

Tuesday, March 01, 2011 12:21 PM by Charlie

I am not sure this is related, but I had problems in XL 2003 where deleting a sheet that had activeX buttons on it caused the VBAIDE to lose state - which meant all my global variables were reset - very annoying

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: