Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Resizing a Text Box in a Macro.

Resizing a Text Box in a Macro

Written by Allen Wyatt (last updated May 13, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


1

Rob has a text box, in a worksheet, that contains text copied from Word. He wants to know how he can resize the text box using a macro, so that it covers a specific range of cells.

There are a couple of ways you can approach this task. One is to specify, in the macro, exactly which cells you want to cover with the text box, and then adjust the properties of the text box to match the characteristics of the cells you specify.

Sub ResizeBox1()
    Dim sTL As String
    Dim sBR As String
    Dim rng As Range

    ' Change top-left and bottom-right addresses as desired
    sTL = "A1"
    sBR = "M40"

    ' Ensure a text box is selected
    If TypeName(Selection) <> "TextBox" Then
        MsgBox "Text box not selected"
        Exit Sub
    End If

    With Selection
        Set rng = ActiveSheet.Range(sTL)
        .Top = rng.Top
        .Left = rng.Left
        Set rng = ActiveSheet.Range(sBR)
        .Width = rng.Left + rng.Width
        .Height = rng.Top + rng.Height
    End With
    Set rng = Nothing
End Sub

In order to use the macro, change the address of the cells you want to use for the top-left and bottom-right of the text box. Then, select the text box and run the macro.

If you prefer, you could use a named range to specify the range to be covered by the text box. The following macro expects that the range will be named RangeToCover. When you select the text box and run the macro, the text box is resized to match the size of the range.

Sub ResizeBox2()
    Dim l_rRangeToCover As Range
    Dim l_rLowerRight As Range

    ' Ensure a text box is selected
    If TypeName(Selection) <> "TextBox" Then
        MsgBox "Text box not selected"
        Exit Sub
    End If

    ' Get the range to cover
    Set l_rRangeToCover = _
      ActiveSheet.Range(Names("RangeToCover").RefersToRange.Value)

    ' Get its lower right cell
    Set l_rLowerRight = _
      l_rRangeToCover.Cells( _
      l_rRangeToCover.Rows.Count, _
      l_rRangeToCover.Columns.Count)

    ' Resize the text box
    With Selection
        .Left = l_rRangeToCover.Left
        .Top = l_rRangeToCover.Top
        .Width = l_rLowerRight.Left + l_rLowerRight.Width - .Left
        .Height = l_rLowerRight.Top + l_rLowerRight.Height - .Top
    End With
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3143) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Resizing a Text Box in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving Common Formulas

It is not uncommon to reuse formulas in a variety of workbooks. If you develop some "gotta keep" formulas, here are some ...

Discover More

Inserting a Cross-Reference to the First Style on a Page

A common way to set up a header is to have it refer to the first occurrence of a heading on the page. (Think how the ...

Discover More

Relative References to Cells in Other Workbooks

When you construct a formula and click on a cell in a different workbook, an absolute reference to that cell is placed in ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Changing an AutoShape

Need to change a shape you previously added to your worksheet? It's easy to do using the graphics tools provided by ...

Discover More

Controlling Display of the Scroll Bars

The scroll bars can be very handy in navigating your worksheet, but did you know you can turn them off or on at will? ...

Discover More

Hiding Graphics when Filtering

Excel allows you to set up graphics so that they are associated with cells and even stay with the cells when the cells ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four minus 0?

2015-10-31 10:52:10

JMJ

Defining "l_rRangeToCover" is rather intricate especially as it's not necessary...
[RangeToCover] (with the square brackets) could be used directly as a replacement in the whole Sub!


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.