Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier 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 14, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

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 (10185) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel 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

Compound List Formatting

Word can help you do quite a bit of complex formatting to your lists, both bulleted and numbered. Using the steps ...

Discover More

Different Table Captions on Multiple Pages

If you have a table that spans multiple pages, you may want to add a caption to the table and have that caption use ...

Discover More

Inconsistent Behavior of Find and Replace Dialog Box

The Find and Replace feature in Excel is one of the workhorse editing tools you can use. When the Find and Replace dialog ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Placing Textbox Text Into a Worksheet

Want to get rid of your text boxes and move their text into the worksheet? It's going to take a macro-based approach, ...

Discover More

Sizing Text Boxes and Cells the Same

Adding a text box to a worksheet is easy. Making sure that text box is the exact size of a cell in the worksheet may not ...

Discover More

Using Text Boxes

Text boxes are handy for placing information in a container that can "float" over your worksheet. This tip explains what ...

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 seven minus 7?

2024-05-19 11:21:00

J. Woolley

You can pick Page Layout > Arrange > Align > Snap to Grid (Alt+P+AA+P), then manually move/resize shapes to align with a range.


2024-05-17 18:54:06

J. Woolley

@Mike J
The Tip's original ResizeBox1 macro can be simplified by replacing the following statements
        .Width = rng.Left + rng.Width
        .Height = rng.Top + rng.Height
with these statements
        .Width = rng.Width
        .Height = rng.Height

Here is a simplified version of the Tip's ResizeBox2 macro:

Sub ResizeBox2()
    Dim rRangeToCover 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 rRangeToCover = ActiveSheet.Range("RangeToCover")
    ' Resize the text box
    With Selection
        .Left = rRangeToCover.Left
        .Top = rRangeToCover.Top
        .Width = rRangeToCover.Width
        .Height = rRangeToCover.Height
    End With
End Sub

Here is another version that works with any visible shape (including Notes, Charts, and Pictures) and is perhaps more user-friendly:

Sub CoverRangeWithShape()
    Const myName As String = "CoverRangeWithShape"
    Const OneSecond As Single = 1 / 24 / 3600
    Dim shp As Shape, rng As Range, msg As String, ans As Variant
    If Not (TypeOf ActiveSheet Is Worksheet) Then Exit Sub
    If ActiveSheet.Shapes.Count = 0 Then Exit Sub
    For Each shp In ActiveSheet.Shapes
        If shp.Visible Then
            shp.Select
            shp.TopLeftCell.Show
            Application.Wait (Now + OneSecond)
            msg = shp.Name & " is selected. " _
                & "Do you want to cover a range with this shape?"
            ans = MsgBox(msg, vbYesNoCancel, myName)
            If ans <> vbNo Then Exit For
        End If
    Next shp
    If ans <> vbYes Then Exit Sub
    msg = "Specify the range address or range name to cover. " _
        & vbLf & "You can use the mouse (or similar device)."
    ans = Range(shp.TopLeftCell, shp.BottomRightCell).Address
    On Error Resume Next
        Set rng = Application.InputBox(msg, myName, ans, Type:=8)
    On Error GoTo 0
    If rng Is Nothing Then Exit Sub
    rng.Cells(1).Show
    With shp
        .Left = rng.Left
        .Top = rng.Top
        .Width = rng.Width
        .Height = rng.Height
    End With
End Sub


2024-05-17 04:33:50

Mike J

@J.Woolley
Thank you for the corrections.
I couldn't get the second macro to work either, but I didn't understand the statement that caused the error, so thought it may be me; especially as Debug/Compile_VBA_Project did not find any problems. Clearly it can't catch them all!


2024-05-16 15:37:42

J. Woolley

@Mike J
The following statements in macro ResizeBox1
        .Width = rng.Left + rng.Width
        .Height = rng.Top + rng.Height
should be replaced with these corrected statements
        .Width = rng.Left + rng.Width - .Left
        .Height = rng.Top + rng.Height - .Top
The Tip's ResizeBox2 macro doesn't work at all. To fix it, replace the following statement
    Set l_rRangeToCover = _
      ActiveSheet.Range(.Names("RangeToCover").RefersToRange.Value)
with this corrected statement
    Set l_rRangeToCover = _
      ActiveSheet.Range("RangeToCover")


2024-05-15 06:35:44

Mike J

The first macro doesn't seem to work correctly.
If top left is set to A1 and bottom right to E5, then the text box correctly occupies 5 rows and 5 columns from A1:E5. But if the top left is C3 and bottom right E5, the text box still occupies 5 rows and 5 columns - from C3:G7


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.