Resize controls within worksheet cells

When you resize columns or rows on a worksheet, any host controls within the cells automatically resize to the height or width of the cell that was resized. Windows Forms controls do not resize automatically by default.

Applies to: The information in this topic applies to document-level projects for Excel. For more information, see Features available by Office application and project type.

If you add the controls at design time, you must set positioning options for each control.

If you add a Windows Forms control programmatically and supply a range argument, the control automatically resizes when a cell within the range is resized. For more information, see Add controls to Office documents at run time.

Resize controls at design time

To make controls resize with cells at design time

  1. From the Toolbox, drag a Windows Forms control to a worksheet.

  2. Right-click the control, and then click Format Control.

  3. In the Format Control dialog box, click the Properties tab.

  4. Under Object Positioning, select the Move and size with cells option, and then click OK.

    When you resize the cell that contains the control, the control resizes to fit the cell.

Resize controls at run time

If you add a Windows Forms control at run time and pass in a Range as the location for the control, the control will automatically resize when the worksheet cell that contains the range is resized.

To make controls resize with cells at run time

  1. Add a control to range A1.

    Microsoft.Office.Tools.Excel.Controls.Button control1 =
        this.Controls.AddButton(this.Range["A1"], "control1");
    

    When you resize the cell that contains the control, the control resizes to fit the cell.

Reset control placement

You can reset the placement and resizing of the control by setting the Placement property to one of the following XlPlacement values:

To change the behavior of a control so that it does not resize or move with the cell

  1. Call the placement property of the control and set the value to xlFreeFloating.

    control1.Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlFreeFloating;