精进ExcelEXCELExcel 加油站

线表计划高级阶段-随心所欲画图形

2019-05-20  本文已影响0人  Excel老崔

 前言

前边两章学了直线的画法,那么其他的图形如何画呢?其实非常简单,既然我们原理都会了,现在就差一个表达式了。

 原理

图形的表达式和直线稍有不同,不再用两个端点的坐标表示,而是用起点坐标和长宽来表示。其实都是一样的,既然我们知道两个端点的坐标,那么长度不就是两个端点的横坐标之差吗?

Activesheet.AddShape 类型, X, Y, Width, Height 

类型是指你想要画什么图形,可以用下面的字符串表示,也可以直接写数字。下面为几个常用的类型。

椭圆:msoShapeOval = 9

矩形:msoShapeRectangle = 1

右箭头:msoShapeRightArrow = 33

下箭头:msoShapeDownArrow = 36

五角星:msoShape5pointStar = 92

 单系列

还是拿我们第一次的简化实例,重新下一遍,这次画一个矩形,代码如下:

Sub drawlineR2()

For i = 2 To 4

    start_x = Cells(i, Cells(i, 4)).Left

    Start_y = Cells(i, Cells(i, 4)).Top + Rows(i).Height / 3

    finish_x = Cells(i, Cells(i, 5)).Left + Cells(i, Cells(i, 5)).Width

    W = finish_x - start_x

    H = Rows(i).Height / 3

    ActiveSheet.Shapes.AddShape(1, start_x, Start_y, W, H).Select

Next

End Sub

 多系列

多个系列,如果日期有重叠,请合理设置每个系列的位置和高度,使之错开一点的间距,否则会重叠在一起,最好使用不同的填充色,这样对比更明显,如下图所示:

代码如下:

Sub drawlineR3()

For i = 2 To 4

'系列一

    start_x = Cells(i, Cells(i, 6)).Left

Start_y = Cells(i, Cells(i, 6)).Top + Rows(i).Height / 4

    finish_x = Cells(i, Cells(i, 7)).Left + Cells(i, Cells(i, 7)).Width

    Finish_y = Start_y

    W = finish_x - start_x

H = Rows(i).Height / 4

    ActiveSheet.Shapes.AddShape(1, start_x, Start_y, W, H).Select

    Selection.ShapeRange.Fill.ForeColor.RGB = vbRed

'系列二

    start_x = Cells(i, Cells(i, 8)).Left

Start_y = Cells(i, Cells(i, 8)).Top + Rows(i).Height / 2

    finish_x = Cells(i, Cells(i, 9)).Left + Cells(i, Cells(i, 9)).Width

    Finish_y = Start_y

    W = finish_x - start_x

H = Rows(i).Height / 4

    ActiveSheet.Shapes.AddShape(1, start_x, Start_y, W, H).Select

Next

End Sub

 里程碑

大家知道里程碑只是一个点,所以只要一个日期就可以了,宽度和高度大家可以根据所在的单元格进行调整,确保在单元格的中心位置就可以了。

代码如下:

Sub drawlineR4()

For i = 2 To 4

'里程碑1

start_x = Cells(i, Cells(i, 4)).Left + Cells(i, Cells(i, 4)).Width / 3

Start_y = Cells(i, Cells(i, 4)).Top + Rows(i).Height / 3

W = Cells(i, Cells(i, 4)).Width / 3

H = Rows(i).Height / 3

ActiveSheet.Shapes.AddShape(36, start_x, Start_y, W, H).Select

    Selection.ShapeRange.Fill.ForeColor.RGB = vbRed

'里程碑2

   start_x = Cells(i, Cells(i, 5)).Left + Cells(i, Cells(i, 5)).Width / 3

    Start_y = Cells(i, Cells(i, 5)).Top + Rows(i).Height / 3

    W = Cells(i, Cells(i, 5)).Width / 3

    H = Rows(i).Height / 3

ActiveSheet.Shapes.AddShape(92, start_x, Start_y, W, H).Select

    Selection.ShapeRange.Fill.ForeColor.RGB = vbRed

Next

End Sub

总结

线表计划你学会了吗?有问题,欢迎在下方留言!

上一篇 下一篇

猜你喜欢

热点阅读