如何用VBA在Excel中画直线? 使用excel vba在cad里面画一条直线的问题
作者&投稿:僪促 (若有异议请与网页底部的电邮联系)
怎么利用EXCEL vba 在CAD上画多段线~
当本方法应用于 Shapes 对象时,返回一个 Shape 对象,该对象代表工作表中的新线条。当本方法应用于 CanvasShapes 对象时,返回一个 Shape 对象,该对象代表绘图画布中的新线条。
语法:
expression.AddLine(BeginX, Beginy, EndX, EndY)
其中:
expression 必选。该表达式返回 Shapes 对象。
BeginX, BeginY Single 类型,必需。相对于文档的左上角,以磅为单位给出线条的起点位置。
EndX, EndY Single 类型,必需。相对于文档的左上角,以磅为单位给出线条的终点位置。
示例:
本示例向 myDocument 中添加蓝色的虚线。
Set myDocument = Worksheets(1)
With myDocument.Shapes.AddLine(10, 10, 250, 250).Line
.DashStyle = msoLineDashDotDot
.ForeColor.RGB = RGB(50, 0, 128)
End With
ActiveSheet.DrawingObjects.Delete
Dim startdate As Date, enddate As Date
Dim l As Single, t As Single, r As Single, sh As Shape
For i = 5 To 24
If Cells(i, 3) > "" Then
startdate = CDate(Replace(Split(Cells(i, 3), "-")(0), ".", "-"))
enddate = CDate(Replace(Split(Cells(i, 3), "-")(1), ".", "-"))
t = (Cells(i, 1).Top + Cells(i + 1, 1).Top) / 2
l = Cells(i, 4 + DateDiff("m", #9/1/2008#, startdate)).Left + Day(startdate) * Cells(i, 4).Width / Day(DateSerial(Year(startdate), Month(startdate) + 1, 0))
r = Cells(i, 4 + DateDiff("m", #9/1/2008#, enddate)).Left + Day(enddate) * Cells(i, 4).Width / Day(DateSerial(Year(enddate), Month(enddate) + 1, 0))
Set sh = ActiveSheet.Shapes.AddLine(l, t, r, t)
sh.Line.Weight = 1.8
sh.Line.ForeColor.RGB = Int(Rnd * &HFFFFFF)
End If
Next
你看看,是否可以学会划线呢
1、复制计算出的坐标到excel中,分为X、Y两列坐标
2、把X、Y坐标用逗号(英文状态)连接在一起,excel里用公式&","&连接
3、选中坐标组,复制(这样数据暂时都保存在粘贴板里)
4、打开cad软件,新建文件或者在你需要绘制的文件里,输入spl命令(或者点击工具栏图标『样条曲线』)
5、直接Ctrl+V粘贴(或者在命令行右键菜单“粘贴”)刚才复制的坐标组,然后回车确定
6、双击滚轮显示全部对象,可以看到曲线已经画好了
这是没有引入相关的定义,一般这样就可以了:
Dim line
当本方法应用于 Shapes 对象时,返回一个 Shape 对象,该对象代表工作表中的新线条。当本方法应用于 CanvasShapes 对象时,返回一个 Shape 对象,该对象代表绘图画布中的新线条。
语法:
expression.AddLine(BeginX, Beginy, EndX, EndY)
其中:
expression 必选。该表达式返回 Shapes 对象。
BeginX, BeginY Single 类型,必需。相对于文档的左上角,以磅为单位给出线条的起点位置。
EndX, EndY Single 类型,必需。相对于文档的左上角,以磅为单位给出线条的终点位置。
示例:
本示例向 myDocument 中添加蓝色的虚线。
Set myDocument = Worksheets(1)
With myDocument.Shapes.AddLine(10, 10, 250, 250).Line
.DashStyle = msoLineDashDotDot
.ForeColor.RGB = RGB(50, 0, 128)
End With
ActiveSheet.DrawingObjects.Delete
Dim startdate As Date, enddate As Date
Dim l As Single, t As Single, r As Single, sh As Shape
For i = 5 To 24
If Cells(i, 3) > "" Then
startdate = CDate(Replace(Split(Cells(i, 3), "-")(0), ".", "-"))
enddate = CDate(Replace(Split(Cells(i, 3), "-")(1), ".", "-"))
t = (Cells(i, 1).Top + Cells(i + 1, 1).Top) / 2
l = Cells(i, 4 + DateDiff("m", #9/1/2008#, startdate)).Left + Day(startdate) * Cells(i, 4).Width / Day(DateSerial(Year(startdate), Month(startdate) + 1, 0))
r = Cells(i, 4 + DateDiff("m", #9/1/2008#, enddate)).Left + Day(enddate) * Cells(i, 4).Width / Day(DateSerial(Year(enddate), Month(enddate) + 1, 0))
Set sh = ActiveSheet.Shapes.AddLine(l, t, r, t)
sh.Line.Weight = 1.8
sh.Line.ForeColor.RGB = Int(Rnd * &HFFFFFF)
End If
Next
你看看,是否可以学会划线呢
《...vba2007中用代码画一条直线带箭头的(箭头和excel2007中手动画的线...》
答:你自己用录制宏的功能,直接画一个所需的箭头线,代码就出来了