Insert Custom Shape in a Cell with Specific Value

phyxe

New Member
Joined
Jan 18, 2016
Messages
7
Hi! I am trying to make a vba code that will insert a custom shape I drew on the sheet. Basically it's just a triangle with the following data
Height: 17.28834724
Width: 13.47456646
Left: 500.2627563
Top: 140.0082703

My sheet looks like this
1587613877586.png

what i am tryig to accomplish is that each cell that contains the letter "A" will have the shape so it will look somewhat like this
1587614085649.png


the way im doing this is manually copying and pasting the shape to the cells. and all i found from my recent searches are vba script that changes the color of an existing shape not insert a new shape, so I am now at my wits end on how to achieve this. Any help is much appreciated, thank you in advance for any advice you may give. ?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Select a cell then run the macro
VBA Code:
Sub MM1()
cl = Selection.Left
ct = Selection.Top
ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, cl, ct, 13.47456646, 17.28834724). _
      Select
  Selection.ShapeRange.IncrementRotation 90
  With Selection.ShapeRange.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorText1
      .Solid
  End With
End Sub
 
Upvote 0
Select a cell then run the macro
VBA Code:
Sub MM1()
cl = Selection.Left
ct = Selection.Top
ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, cl, ct, 13.47456646, 17.28834724). _
      Select
  Selection.ShapeRange.IncrementRotation 90
  With Selection.ShapeRange.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorText1
      .Solid
  End With
End Sub

thanks I will this one out :)
 
Upvote 0
I was able to make it work by combining @Michael M suggestion and some codes I found here

so all in all here's the complete code
VBA Code:
Sub rangeShapeInsert(myRange As Range)
    
    Dim posLeft As Long
    Dim posTop As Long
    Dim posWidth As Long
    Dim posHeight As Long
    Dim myShape As Shape
    
    posLeft = myRange.Left
    posTop = myRange.Top
    posWidth = myRange.Width
    posHeight = myRange.Height
    
    With myRange.Parent
        Set myShape = .Shapes.AddShape(msoShapeRightTriangle, posLeft, posTop, posWidth, posHeight)
        myShape.Flip msoFlipVertical
        With myShape.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorText1
            .Solid
        End With
    End With
End Sub

Sub InsertShapes()
    
    Dim myCell As Range
    Dim cl As Range
    
    For Each myCell In ActiveSheet.Range("") 'insert static range of cells
        If myCell.Value2 = "A" Then
            RangeToLateShape myCell
            myCell.Font.TintAndShade = 0
        ElseIf myCell.Value2 = "B" Then
            RangeToCuttingClassShape myCell
            myCell.Font.TintAndShade = 0
        ElseIf myCell.Value2 = "C" Then
            Absent myCell
            myCell.Font.TintAndShade = 0
        End If
    Next myCell
        
End Sub

thank you again for the help :)
 
Upvote 0
Glad you managed to get a positive result...?
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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