Jeffrey Mahoney
Well-known Member
- Joined
- May 31, 2015
- Messages
- 3,142
- Office Version
- 365
- Platform
- Windows
I've struggled with this for a long time and I finally found a way to solve it and it was right under my nose the whole time. When I recorded a macro to change the size of the textbox it provided the .scaleheight method.
I was trying to use ActiveSheet.Shapes(TBName).Height = TextArea.Height. But this most always was a few points off. This is true for setting the width this way. Below I calculate a ratio of target height compared to the textbox height with the scaleheight function and it works every time.
Does anybody know why setting the height or width directly produces random differences? When I changed my zoom setting just right I could get it to the target height.
I was trying to use ActiveSheet.Shapes(TBName).Height = TextArea.Height. But this most always was a few points off. This is true for setting the width this way. Below I calculate a ratio of target height compared to the textbox height with the scaleheight function and it works every time.
Does anybody know why setting the height or width directly produces random differences? When I changed my zoom setting just right I could get it to the target height.
VBA Code:
Sub ResizeTextBoxes()
Dim TextArea As Range
Dim TBName As String
Dim Ratio As Single
TBName = "ProblemTB" 'Name of textbox
Set TextArea = Range("ProblemTextArea") 'Textbox needs to be same size and location as range
With ActiveSheet.Shapes(TBName)
.LockAspectRatio = msoFalse 'Allow height and width to be flexible
.Left = TextArea.Left 'Set left edge to range
.Top = TextArea.Top 'Set top edge to range
Ratio = TextArea.Height / .Height 'Height cannot directly be set for some reason
.ScaleHeight Ratio, msoFalse, msoScaleFromTopLeft 'Using ratio to set the scale height
Ratio = TextArea.Width / .Width 'Width cannot directly be set for some reason
.ScaleWidth Ratio, msoFalse, msoScaleFromTopLeft 'Using ratio to set the scale width
End With
TBName = "RefCaseTB"
Set TextArea = Range("RefCaseTextArea")
With ActiveSheet.Shapes(TBName)
.LockAspectRatio = msoFalse
.Left = TextArea.Left
.Top = TextArea.Top
Ratio = TextArea.Height / .Height
.ScaleHeight Ratio, msoFalse, msoScaleFromTopLeft
Ratio = TextArea.Width / .Width
.ScaleWidth Ratio, msoFalse, msoScaleFromTopLeft
End With
TBName = "BusinessCaseTB"
Set TextArea = Range("BusinessCaseTextArea")
With ActiveSheet.Shapes(TBName)
.LockAspectRatio = msoFalse
.Left = TextArea.Left
.Top = TextArea.Top
Ratio = TextArea.Height / .Height
.ScaleHeight Ratio, msoFalse, msoScaleFromTopLeft
Ratio = TextArea.Width / .Width
.ScaleWidth Ratio, msoFalse, msoScaleFromTopLeft
End With
TBName = "KeyRiskTB"
Set TextArea = Range("KeyRiskTextArea")
With ActiveSheet.Shapes(TBName)
.LockAspectRatio = msoFalse
.Left = TextArea.Left
.Top = TextArea.Top
Ratio = TextArea.Height / .Height
.ScaleHeight Ratio, msoFalse, msoScaleFromTopLeft
Ratio = TextArea.Width / .Width
.ScaleWidth Ratio, msoFalse, msoScaleFromTopLeft
End With
End Sub