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

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

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:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
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.
    Chairil · 6 months ago
    Hi Crytal
    what if to determine the side of the cube, triangle, box that must be determined based on the length, width? Please help me

    Thank You
    chairil
    • To post as a guest, your comment is unpublished.
      crystal · 5 months ago
      Hi Chairil,
      Sorry can't help you with that yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    carol gomezgianine · 7 months 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 · 1 years 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 · 2 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 · 2 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 · 2 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 · 2 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
  • To post as a guest, your comment is unpublished.
    Ranjit Konkar · 2 years ago
    How do I name my shape? In your example above, how do you assign the name Oval 2 to the circle you have drawn?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Ranjit,
      For naming a shape, please select this shape, enter the shape name into the Name Box, and then press the Enter key. See below image shown.
  • To post as a guest, your comment is unpublished.
    Jade · 2 years ago
    How would you execute this with multiple shapes each depending on different cells?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Jade,
      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