Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to auto change shape size based/dependent on specified cell value in Excel?

If you want to automatically change the shape size based on the value of a specified cell, this article can help you.

Auto change shape size based on specified cell value with VBA code


List and export all shapes in current Excel workbook:

The Export Graphics utility of Kutools for Excel help you quickly list all shapes in current workbook, and you can export them all to a certain folder at once. See screenshot:

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now


Auto change shape size based on specified cell value with VBA code


The following VBA code can help you to change a certain shape size based on specified cell value in current worksheet. Please do as follows.

1. Right click the sheet tab with shape you need to change size, and then click View Code from the right-clicking menu.

2. In the Microsoft Visual Basic for Applications window, copy and paste the following VBA code into the Code window.

VBA code: Auto change shape size based on specified cell value in Excel

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target.Row = 2 And Target.Column = 1 Then
        Call SizeCircle("Oval 2", Val(Target.Value))
    End If
End Sub
Sub SizeCircle(Name As String, Diameter)
    Dim xCenterX As Single
    Dim xCenterY As Single
    Dim xCircle As Shape
    Dim xDiameter As Single
    On Error GoTo ExitSub
    xDiameter = Diameter
    If xDiameter > 10 Then xDiameter = 10
    If xDiameter < 1 Then xDiameter = 1
    Set xCircle = ActiveSheet.Shapes(Name)
    With xCircle
        xCenterX = .Left + (.Width / 2)
        xCenterY = .Top + (.Height / 2)
        .Width = Application.CentimetersToPoints(xDiameter)
        .Height = Application.CentimetersToPoints(xDiameter)
        .Left = xCenterX - (.Width / 2)
        .Top = xCenterY - (.Height / 2)
    End With
ExitSub:
End Sub

Note: In the code, “Oval 2” is the shape name you will change its size. And Row = 2, Column = 1 means that the size of shape “Oval 2” will be changed with the value in A2. Please change them as you need.

For auto resizing multiple shapes based on different cell values, please apply the below VBA code.

VBA code: Auto resize multiple shapes based on different specified cells' value in Excel

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xAddress As String
    On Error Resume Next
    If Target.CountLarge = 1 Then
        xAddress = Target.Address(0, 0)
        If xAddress = "A1" Then
            Call SizeCircle("Oval 1", Val(Target.Value))
        ElseIf xAddress = "A2" Then
            Call SizeCircle("Smiley Face 3", Val(Target.Value))
        ElseIf xAddress = "A3" Then
            Call SizeCircle("Heart 2", Val(Target.Value))
        End If
    End If
End Sub

Sub SizeCircle(Name As String, Diameter)
    Dim xCenterX As Single
    Dim xCenterY As Single
    Dim xCircle As Shape
    Dim xDiameter As Single
    On Error GoTo ExitSub
    xDiameter = Diameter
    If xDiameter > 10 Then xDiameter = 10
    If xDiameter < 1 Then xDiameter = 1
    Set xCircle = ActiveSheet.Shapes(Name)
    With xCircle
        xCenterX = .Left + (.Width / 2)
        xCenterY = .Top + (.Height / 2)
        .Width = Application.CentimetersToPoints(xDiameter)
        .Height = Application.CentimetersToPoints(xDiameter)
        .Left = xCenterX - (.Width / 2)
        .Top = xCenterY - (.Height / 2)
    End With
ExitSub:
End Sub

Notes:

1) In the code, “Oval 1”, “Smiley Face 3” and “Heart 3” are the shapes' name you will change their sizes automatically. And A1, A2 andA3 are the cells which values you will auto resize shapes based on.

2) If you want to add more shapes, please add lines "ElseIf xAddress = "A3" Then" and "Call SizeCircle("Heart 2", Val(Target.Value))" above the first "End If" line in the code. And change the cell address and shape name based on your needs.

3. Press Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.

From now on, when you change the value in cell A2, the size of shape Oval 2 will be changed automatically. See screenshot:

Or change the values in cell A1, A2 and A3 to resize the corresponding shapes "Oval 1", "Smiley Face 3" and "Heart 3" automatically. See screenshot:

Note: The shape size will no longer change when the cell value is greater than 10.


Related articles:


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    carol gomezgianine · 25 days ago
    Hi Crytal,

    I would like to ask you, if there is a way to select color (red cell = red form) and name from specific cells . could it also be possible to create forms automatically from VBA?

    Thank you so much in advance :)

    Carol


  • To post as a guest, your comment is unpublished.
    Andrew · 5 months ago
    Is there a way to do this with Images? I don't seem to be having any luck using the code as posted.

    5 Images in a leaderboard, I want the Images in 1st or tied for 1st to be larger. Therefore I've 2 fixed image sizes, either 1x2 for not first or 2x4 for 1st placed (for example). I've got ranking already set-up so can use that to create sizes in specific cells for each image (ie use an IF statement so IF RANK is 1st size width is 2). My VBA is pretty weak though.

    Basically I want - on sheet update - look at image size cells and set each image size to the specific image size cells result. I can't see in the VBA above how that exactly works but I think it should be easy!
  • To post as a guest, your comment is unpublished.
    Sam · 1 years ago
    Hi, is there a way that I can make the shape expand on two dimensions (instead of increasing the shape size by 5, increase it 5 on the horizontal and 3 on the vertical)?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Sam,
      The following VBA script can help you solve the problem. And the two dimensions are cell A1 and B1.

      Private Sub Worksheet_Change(ByVal Target As Range)
      On Error Resume Next
      If Target.Count = 1 Then
      If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
      Call SizeCircle("Oval 2", Array(Val(Range("A1").Value), Val(Range("B1").Value)))
      End If
      End If
      End Sub
      Sub SizeCircle(Name As String, Arr As Variant)
      Dim I As Long
      Dim xCenterX As Single
      Dim xCenterY As Single
      Dim xCircle As Shape
      On Error GoTo ExitSub
      For I = 0 To UBound(Arr)
      If Arr(I) > 10 Then
      Arr(I) = 10
      ElseIf Arr(I) < 1 Then
      Arr(I) = 1
      End If
      Next
      Set xCircle = ActiveSheet.Shapes(Name)
      With xCircle
      xCenterX = .Left + (.Width / 2)
      xCenterY = .Top + (.Height / 2)
      .Width = Application.CentimetersToPoints(Arr(0))
      .Height = Application.CentimetersToPoints(Arr(1))
      .Left = xCenterX - (.Width / 2)
      .Top = xCenterY - (.Height / 2)
      End With
      ExitSub:
      End Sub
  • To post as a guest, your comment is unpublished.
    Ian · 1 years ago
    Hi,
    I have tried to use your post to write my own VBA code but don't seem to be getting very far. Mainly because I don't really understand VBA and I'm just trying to adapt your. I was wondering if you could help. I am wanting to change the length of a rectangle depending on the value in a cell. I would like the width if the rectangle to stay the same but the length to change. I would like both left hand vertices to stay in the same place and it to lengthen to the right. Is this possible?
    Thank you
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear lan,
      Hope the following VBA code can solve your problem. (Please replace the Oval 1 with the shape name of your own)

      Private Sub Worksheet_Change(ByVal Target As Range)
      On Error Resume Next
      If Target.Row = 2 And Target.Column = 1 Then
      Call SizeCircle("Oval 1", Val(Target.Value))
      End If
      End Sub
      Sub SizeCircle(Name As String, Diameter)
      Dim xCircle As Shape
      Dim xDiameter As Single
      On Error GoTo ExitSub
      xDiameter = Diameter
      If xDiameter > 10 Then xDiameter = 10
      If xDiameter < 1 Then xDiameter = 1
      Set xCircle = ActiveSheet.Shapes(Name)
      xCircle.ScaleWidth 1.5, msoFalse, msoScaleFromTopLeft
      With xCircle
      .LockAspectRatio = msoFalse
      .Width = Application.CentimetersToPoints(xDiameter)
      End With
      ExitSub:
      End Sub
  • To post as a guest, your comment is unpublished.
    Abhinaya · 1 years ago
    Hi, how do i replicate the same for multiple shapes linked to multiple cells in the same module?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Abhinaya,
      The article is updated with a new code section which can help you to execute with multiple shapes each depending on different cells. Thank you for your comment.

      Best Regards,
      Crystal