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.
Written by Allen Wyatt (last updated May 9, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
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!
You probably know that text boxes can contain text. (Else why call them text boxes?) Did you know that you could make ...
Discover MoreText boxes are handy for placing information in a container that can "float" over your worksheet. This tip explains what ...
Discover MoreOne way to make your text boxes "stand off" the page is to add a drop shadow to them. This tip shows just how easy it is ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments