Positioning of form controls using VBA code – Bug in Excel 2010?

When testing an own developped VBA application in Excel 2010, I noticed a curiosity, which I personally would classify as a bug in Excel 2010.
My application is using some form controls, especially checkboxes which are hidden or unhidden per code. And the cells containing the controls are also hidden or unhidden per code.
The error in Excel 2010 appears when following steps are done:
  • Hide the form controls per code (Visible = False).
  • Hide the rows containing the controls.
  • Save and close the workbook.
  • Open the workbook again.
  • The form controls are now all on top and lost their original positions.
This problem does not appear in Excel 2007. Now, for getting sure that this is actually a problem of Excel 2010, I wrote a small sample application, which positioned 9 checkboxes as shown in the screenshots below. Please note, that I haved used the german version of Excel for the screenshots as the english version of Excel 2010 is not available yet for me. The properties for each control are set to “Move but don’t size with cells” (in German “Nur von Zellposition abhängig”). The two screenshots below show the worksheet in Excel 2007 and Excel 2010 in the initial state without any influence from VBA code.


 Then I created the two procedures „HideCheckboxes()“ and „ShowCheckboxes()“ in a module and linked them to the two buttons. The relative simple code just hides or unhides the controls and the rows.
  1. Public Sub HideCheckboxes()  
  2.     
  3.   Dim n As Long  
  4.     
  5. ' With...  
  6.     
  7.   With ThisWorkbook.ActiveSheet  
  8.     
  9. '   Loop...  
  10.       
  11.     For n = 1 To .Shapes.Count  
  12.         
  13.       If Not .Shapes(n).FormControlType <> 1 Then  
  14.           
  15.        .Shapes(n).Visible = False  
  16.           
  17.       End If  
  18.         
  19.     Next n  
  20.       
  21. '   Hide...  
  22.       
  23.    .Range("$2:$12").EntireRow.Hidden = True  
  24.     
  25.   End With  
  26.     
  27. ' Save...  
  28.     
  29.   ThisWorkbook.Save  
  30.     
  31. End Sub  
  32.   
  33. Public Sub ShowCheckboxes()  
  34.   
  35.   Dim n As Long  
  36.     
  37. ' With...  
  38.     
  39.   With ThisWorkbook.ActiveSheet  
  40.       
  41. '   Loop...  
  42.       
  43.     For n = 1 To .Shapes.Count  
  44.         
  45.       If Not .Shapes(n).FormControlType <> 1 Then  
  46.           
  47.        .Shapes(n).Visible = True  
  48.           
  49.       End If  
  50.         
  51.     Next n  
  52.     
  53. '   Hide...  
  54.         
  55.    .Range("$2:$12").EntireRow.Hidden = False  
  56.     
  57.   End With  
  58.     
  59. ' Save...  
  60.   
  61.   ThisWorkbook.Save  
  62.     
  63. End Sub 

 If the procedure linked to the button „Hide“ is called and the workbook saved, closed and reopened, the you can see the results as shown in the following screenshots for Excel 2007 and Excel 2010.

 As you can see, all controls are stacked in Excel 2010. If you display the property „TopLeftCell“ for each control in a MsgBox, you will notice that in Excel 2007 all values ($C$3, $C$4, $C$5, …) are correct. Excel 2010 overwrites this property to $C$2 directly after hiding the controls and if you save and close the workbook, the value $C$2 is saved and all previous values are lost. This logically leads to an incorrect positioning of the controls when openening the file again.

An interesting behavious is that „TopLeftCell“ can be restored unless the workbook is not saved and closed.
The file is stored in the Open XML Format, therefore we can rename the file to *.zip and decompress the file with an unzip application. Depending on whether the file was created with Excel 2007 or Excel 2010, the archive contains different files a folder called „drawings“. The next screenshot shows an excerpt of the content of the two files „drawing1.xml“ and „vmlDrawing1.vml“ for the Excel 2010.


Let have look to the „vmlDrawing1.vml“ file, if you open the file with an text editor you can easily find the sections for the checkboxes.

The word „Anchor“ suggests the cell that is used for positioning the checkbox. And if we compare the values for „Anchor“ from the file for Excel 2007 with the values from the file for Excel 2010, we can notice that after hiding the controls in Excel 2010, all values have been set to „2, 0, 1, 0, 4, 0, 13, 0“. In the file for Excel 2007, these values are „2, 0, 2, 0, 4, 0, 13, 0“ for the first checkbox, „2, 0, 3, 0, 4, 0, 13, 0“ for the second checkbox and so on.
If you manually restore these values in the file „vmlDrawing1.vml“ for Excel 2010, then rebuild a zip file, rename it to a XLSX file and open the file in Excel 2007, you will notice that all the positions of the form controls are restaured. However, if you open the file in Excel 2010, nothing is restaured. I think, the „vmlDrawing1.vml“ file remained for compatibilty reasons in Excel 2010.
The file „drawing1.xml“ also contains some values for the checkboxes, but also the file „sheet1.xml“ for the first sheet in the „worksheets“. The values differs from the values for unhidden controls. I had experimented a little by changing some values to the values for unhidden controls. Unfortunately it was not possible for me to restore the original positions for the controls.
Finally I tested some other form controls. Excel 2010 has the same behaviour if you use comboboxes, radiobuttons or buttons. If you use buttons, it may happen that the buttons are reduced to a line, so someone may think they were removed.
If this behavious was not intentionally developped by the Office 2010 developers, then it’s a bug. Especially since this behavior has from my point of view no significant advantages.
You can avoid this problem by storing the position of the controls an a data sheet, for example, and, when unhidung the controls, set their original positions per code.
Tag : Tech Guides
! Article Title "Positioning of form controls using VBA code – Bug in Excel 2010?"

Back To Top