Excel 2003/2007autosize textbox width to match cell width

grady121

Active Member
Joined
May 27, 2005
Messages
385
Office Version
  1. 2016
Platform
  1. Windows
I have a code that is used to enter a 'Single line' Textbox onto the worksheet, which works great in XL2003. But not in XL2007.

The end user could be using either XL2003 or 2007, so I was hoping that the width of the new Textbox would be set by the column width, (which is set to Autofit) but in XL2007, I end up with shortened textbox with the text on two lines?

Code:

MyText = InputBox("Enter text", "Single Line Text")

Set Cell = ActiveSheet.Range("BZ1")
Cell.Value = MyText
Columns("BZ:BZ").EntireColumn.AutoFit
Rows("1:1").EntireRow.AutoFit


ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 40, 40, 40, 40).Select 'Create a Textbox

With Selection

.Left = Cell.Left
.TOP = Cell.TOP
.Height = Cell.Height
.Width = Cell.Width

.Characters.Text = Cell.Value
.Border.Color = vbBlack
.Font.Color = vbBlack
.Font.Name = "Arial"
.Font.FontStyle = "Regular"

End With


Can anyone offer any advice to a Newbie.

Thanks in advance.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
For Excel 2010, the textbox has an internal margin. So it's likely that 2007 also has an internal margin, whereas 2003 probably does not. If this is the case, you'll need to include code to check which version of Excel is being used, and then set the internal margin for left and right to 0 when the version being used is 2007 (or later). Or, alternatively, I guess you could probably replace...

Code:
.Width = Cell.Width

with

Code:
.Width = Cell.Width * 1.1
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top