Results 1 to 2 of 2

Thread: VBA To Autosize Shape Based On Length Of Text

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Lightbulb VBA To Autosize Shape Based On Length Of Text

    In the selection_change event i add a shape in the sheet and text within the shape, the text length is different in different selection of cells, in this process I have been trying to auto-size (/autofit) the shape by increasing it's height according to the text length so that user can read the whole text, but cannot do so, The code that i used is increasing it's length horizontally and covering the row that has fields underneath :

    Code:
    Sub Addshape()
    Dim ShpLeft As Double
    Dim ShpTop As Double
    Dim ShpWidth As Double
    Dim ShpHeight As Double
    Dim HelpLkupRng As Range
    Dim HelpLkupVal
    Dim Shp As Range
    Dim sh As Shape
    
    On Error Resume Next
    ActiveSheet.Shapes("MyShapes").Delete
    If Left(ActiveCell.Address, 2) = "$E" Then
    '    ActiveSheet.Shapes("MyShapes").Delete
        Set HelpLkupRng = Worksheets("Config").Range("Z1:AA300")
        HelpLkupVal = WorksheetFunction.VLookup(ActiveSheet.Range("C" & ActiveCell.Row).value, HelpLkupRng, 2, 0)
        If HelpLkupVal = "" Then Exit Sub
        
        Set Shp = Range(Selection.Address)
        
        ShpLeft = 340
        ShpTop = Shp.Top - 2
        ShpHeight = Shp.Height
        ShpWidth = Shp.Width
        
        ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, ShpLeft, ShpTop, 300, 55).Select
        Selection.ShapeRange.ShapeStyle = msoShapeStylePreset25
        With Selection.ShapeRange.TextFrame2.TextRange.Font
            .NameComplexScript = "Arial"
            .NameFarEast = "Arial"
            .Name = "Arial"
        End With
         
        Selection.Name = "MyShapes"
        Selection.Characters.Text = HelpLkupVal
        'Selection.AutoSize = True
        Selection.AutoSize = xlHorizontal
    
    'I tried with "Selection.autosize=true" and "selection.autosize=xlveritcal" , anyway the shape is increasing in length horizontally and not the height
        
        Debug.Print Shp.Left = ShpLeft
        Debug.Print Shp.Top = ShpTop
        ActiveSheet.Range("E" & ActiveCell.Row).Select
    End If
    End Sub
    Thanks ...

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    this worked for me. (Excel 2010 on Win 8 64 bit)

    Code:
    Selection.Name = "MyShapes"
        Selection.Characters.Text = HelpLkupVal
        Selection.ShapeRange.LockAspectRatio = msoTrue
        Selection.ShapeRange.TextFrame2.AutoSize = msoAutoSizeShapeToFitText
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 9
    Last Post: 08-02-2013, 07:55 PM
  2. Replies: 1
    Last Post: 07-19-2013, 08:23 PM
  3. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  4. Macro to check values based on certain text
    By Howardc in forum Excel Help
    Replies: 25
    Last Post: 11-05-2012, 09:03 PM
  5. Replies: 2
    Last Post: 10-20-2011, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •