在不使用SELECT的情况下插入形状

4

我的代码会遍历一系列单元格,如果某个单元格满足特定的条件,就在该单元格中插入形状。虽然它可以正常工作,但我想找到另一种方法来避免使用 select 。

'above - code to find satisfying cell
ActWS.Activate       'Activate Sheet
ActWS.Cells(rActPlan - 1, vReturnColumn).Select 'Select satisfying cell
ActiveSheet.Shapes.AddShape(msoShapeOval, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
Selection.ShapeRange.Fill.Visible = msoFalse
 With Selection.ShapeRange.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 255, 0)
        .Weight = 2.25
 End With
2个回答

5

这段代码将删除所有的 .SelectActiveCell 引用:

With ActWs

    Dim rng as Range
    Set rng = .Cells(rActPlan - 1, vReturnColumn)

    Dim shp as Shape
    Set shp = .Shapes.AddShape(msoShapeOval, rng.Left, rng.Top, rng.Width, rng.Height)

    With shp.ShapeRange

        .Fill.Visible = msoFalse

        With .Line
           .Visible = msoTrue
           .ForeColor.RGB = RGB(0, 255, 0)
           .Transparency = 0
           .Weight = 2.25
        End With

    End With

End With

3

AddShape() 返回刚添加的图形对象,因此您应该能够使用类似下面的代码 - 然后引用shp而不是Selection

Dim shp as Shape
Set shp = ActiveSheet.Shapes.AddShape(msoShapeOval, ActiveCell.Left, _
                   ActiveCell.Top, ActiveCell.Width, ActiveCell.Height)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接