要用vba在工作表中添加直线,可以使用Shapes对象的AddLine方法。
以下代码举例说明了用AddLine方法添加直线:
Sub QQ1722187970() Dim oSP As Shape Dim oWK As Worksheet Set oWK = Excel.ActiveSheet '先删除存在的直线 For Each oSP In oWK.Shapes With oSP If .Type = msoLine Then .Delete End If End With Next '添加直线 Set oSP = oWK.Shapes.AddLine(0, 0, 100, 100) With oSP Debug.Print .Name End With End Sub
除了AddLine方法还可以使用AddConnector方法。
以下代码举例说明了用AddConnector方法添加直线:
Sub QQ1722187970() Dim oSP As Shape Dim oWK As Worksheet Set oWK = Excel.ActiveSheet '先删除存在的直线 For Each oSP In oWK.Shapes With oSP If .Type = msoLine Then .Delete End If End With Next '添加直线 Set oSP = oWK.Shapes.AddConnector(msoConnectorStraight, 0, 0, 100, 100) With oSP Debug.Print .Name End With End Sub
用AddConnector方法除了可以添加直线,还可以添加曲线。
发表评论