VBA在Excel单元格的整数函数中添加形状星号

问题描述

我在电子表格上有一个“ I”列,其整数从1到5。在整数/大于5的函数中,我设法在单元格内的整数旁边显示星星:

Dim x As Integer,Cel As Range,Plg As Range
Dim y As Integer,etoile As Shape
Dim shp As Shape

With mysheet
    Set Plg = .Range("I" & startLine & ":I" & nbLines)

    For Each Cel In Plg
        y = 5
        For x = 1 To Cel.Value
            If Cel > 0 Then
        Set stars= .Shapes.AddShape(msoShape5pointStar,Cel.Left + y,Cel.Top + 5,6,6)
        y = y + 10
        stars.Line.Visible = msoFalse
        stars.Fill.ForeColor.SchemeColor = 13
            End If
        Next x
    Next Cel
End With

但是当我使用刷新我的列和整数的程序时,我正在寻找一种方法删除之前创建的星星,然后应用新的排名/星星数。

我在代码之前尝试过类似的事情:

For Each shp In mysheet.Shapes
    If shp.Type = msoShapeTypeMixed Then shp.Delete
Next shp

没有很大的成功... 任何帮助将不胜感激!

解决方法

请尝试以下操作:

   Dim sh As Worksheet,st As Shape
   Set sh = ActiveSheet
     For Each st In sh.Shapes
        If st.Type = 1 And left(st.Name,7) = "5-Point" Then st.Delete
     Next

您还可以在创建星星时为其命名。

'your existing code
'
Set stars= .Shapes.AddShape(msoShape5pointStar,Cel.Left + y,Cel.Top + 5,6,6)
       y = y + 10
 stars.Name = "MyStar" & "somethin else" '(maybe the Cel.Addres and use it in a similar way to delete only specific ones)
'your existing code

然后删除具有前6个字符的形状,例如“ MyStar”或您喜欢的任何形状...

如果需要有选择地删除它们,也可以确定其TopLeftCell地址并根据该地址进行删除。