前两天看到自动化办公老师写的帖子,深受启发,收藏后深入学习,代码微调了一下,把趋势线的颜色给加上,现在发到群里,请大家批评指正。其中波段顶点和底点的一块代码还有问题,还在学习改进中,等调试好后,再请大家指点。通过VBA在excel中实现股票历史数据查询和K线趋势图绘制(完整的excel原件可以在我的资源中下载)_excel获取股票历史数据-CSDN博客
Sub 绘图()
Dim X1, i, Lx, Ly
Dim Fy1, rFx1, rFy1
Dim Arr, endrow
endrow = [A1].End(xlDown).Row
Arr = Range("A1", Cells(endrow, "I"))
For i = 2 To endrow
X1 = 5 * i
Lx = 400 + X1
ActiveSheet.Shapes.AddLine Lx, 700 - Arr(i, 4) * 16, Lx, 700 - Arr(i, 3) * 16
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 400 + X1 - 1.5, 700 - WorksheetFunction.Max(Arr(i, 2), _
Arr(i, 5)) * 16, 3, Abs(Arr(i, 5) - Arr(i, 2)) * 16).Select
With Selection.ShapeRange
If Arr(i, 5) - Arr(i, 2) >= 0 Then
.Fill.ForeColor.SchemeColor = 10
Else
.Fill.ForeColor.SchemeColor = 11
End If
End With
If Arr(i, 6) <> "" Then
Fy1 = 700 - Arr(i, 6) * 16
If rFy1 > 0 Then
With ActiveSheet.Shapes.AddLine(rFx1, rFy1, Lx, Fy1).Line
.ForeColor.RGB = RGB(0, 255, 0)
End With
End If
rFx1 = Lx
rFy1 = Fy1
End If
If Arr(i, 7) <> "" Then
Fy1 = 700 - Arr(i, 7) * 16
If rFy1 > 0 Then
With ActiveSheet.Shapes.AddLine(rFx1, rFy1, Lx, Fy1).Line
.ForeColor.RGB = RGB(255, 0, 0)
End With
End If
rFx1 = Lx
rFy1 = Fy1
End If
Next
End Sub